Php doesn't catch next array element












0














I have this array :



array (
'cfop' => '3',
'cst' => NULL,
'cstPis' => '2',
'cstCofins' => '1',
)


And this code:



while ($t = current($taxes))
{
$getkey = key($taxes);
Log::debug($getkey);

//$this->saveTaxes($getkey, $t, $add);

$t = next($taxes);
}


ps: $taxes is the array



For some reason the next function doenst not pass the null value, so the log prints just cfop.



If i change the order like this



array (
'cfop' => '3',
'cst' => '2',
'cstPis' => NULL,
'cstCofins' => '1',
)


Logs will print cfop and cst but it does not reach the next value after NULL










share|improve this question




















  • 6




    Because NULL is considered falsy and your iterations stops.
    – u_mulder
    Nov 19 '18 at 15:24






  • 1




    Any reason to not just use a foreach loop here instead?
    – iainn
    Nov 19 '18 at 15:25










  • We might not have your full code but this code snippet could be very much simplified by a simple foreach loop
    – milo526
    Nov 19 '18 at 15:25
















0














I have this array :



array (
'cfop' => '3',
'cst' => NULL,
'cstPis' => '2',
'cstCofins' => '1',
)


And this code:



while ($t = current($taxes))
{
$getkey = key($taxes);
Log::debug($getkey);

//$this->saveTaxes($getkey, $t, $add);

$t = next($taxes);
}


ps: $taxes is the array



For some reason the next function doenst not pass the null value, so the log prints just cfop.



If i change the order like this



array (
'cfop' => '3',
'cst' => '2',
'cstPis' => NULL,
'cstCofins' => '1',
)


Logs will print cfop and cst but it does not reach the next value after NULL










share|improve this question




















  • 6




    Because NULL is considered falsy and your iterations stops.
    – u_mulder
    Nov 19 '18 at 15:24






  • 1




    Any reason to not just use a foreach loop here instead?
    – iainn
    Nov 19 '18 at 15:25










  • We might not have your full code but this code snippet could be very much simplified by a simple foreach loop
    – milo526
    Nov 19 '18 at 15:25














0












0








0







I have this array :



array (
'cfop' => '3',
'cst' => NULL,
'cstPis' => '2',
'cstCofins' => '1',
)


And this code:



while ($t = current($taxes))
{
$getkey = key($taxes);
Log::debug($getkey);

//$this->saveTaxes($getkey, $t, $add);

$t = next($taxes);
}


ps: $taxes is the array



For some reason the next function doenst not pass the null value, so the log prints just cfop.



If i change the order like this



array (
'cfop' => '3',
'cst' => '2',
'cstPis' => NULL,
'cstCofins' => '1',
)


Logs will print cfop and cst but it does not reach the next value after NULL










share|improve this question















I have this array :



array (
'cfop' => '3',
'cst' => NULL,
'cstPis' => '2',
'cstCofins' => '1',
)


And this code:



while ($t = current($taxes))
{
$getkey = key($taxes);
Log::debug($getkey);

//$this->saveTaxes($getkey, $t, $add);

$t = next($taxes);
}


ps: $taxes is the array



For some reason the next function doenst not pass the null value, so the log prints just cfop.



If i change the order like this



array (
'cfop' => '3',
'cst' => '2',
'cstPis' => NULL,
'cstCofins' => '1',
)


Logs will print cfop and cst but it does not reach the next value after NULL







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 20:53









Salman Zafar

8571619




8571619










asked Nov 19 '18 at 15:22









SkullFire

275




275








  • 6




    Because NULL is considered falsy and your iterations stops.
    – u_mulder
    Nov 19 '18 at 15:24






  • 1




    Any reason to not just use a foreach loop here instead?
    – iainn
    Nov 19 '18 at 15:25










  • We might not have your full code but this code snippet could be very much simplified by a simple foreach loop
    – milo526
    Nov 19 '18 at 15:25














  • 6




    Because NULL is considered falsy and your iterations stops.
    – u_mulder
    Nov 19 '18 at 15:24






  • 1




    Any reason to not just use a foreach loop here instead?
    – iainn
    Nov 19 '18 at 15:25










  • We might not have your full code but this code snippet could be very much simplified by a simple foreach loop
    – milo526
    Nov 19 '18 at 15:25








6




6




Because NULL is considered falsy and your iterations stops.
– u_mulder
Nov 19 '18 at 15:24




Because NULL is considered falsy and your iterations stops.
– u_mulder
Nov 19 '18 at 15:24




1




1




Any reason to not just use a foreach loop here instead?
– iainn
Nov 19 '18 at 15:25




Any reason to not just use a foreach loop here instead?
– iainn
Nov 19 '18 at 15:25












We might not have your full code but this code snippet could be very much simplified by a simple foreach loop
– milo526
Nov 19 '18 at 15:25




We might not have your full code but this code snippet could be very much simplified by a simple foreach loop
– milo526
Nov 19 '18 at 15:25












1 Answer
1






active

oldest

votes


















5














null is a falsy value, so when you do $t = current($taxes), that evaluates to something that is falsy, so that's why it stops executing.



If you want to iterate over each value, I'd advise a foreach loop instead.



foreach ($taxes as $key => $value) {
Log::debug($key);
// $this->saveTaxes($key, $value, $add);
}





share|improve this answer





















  • but if i want to take the key name?
    – SkullFire
    Nov 19 '18 at 15:28






  • 3




    @SkullFire You can access the key by using $key from Chris' example.
    – Dirk Scholten
    Nov 19 '18 at 15:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377733%2fphp-doesnt-catch-next-array-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














null is a falsy value, so when you do $t = current($taxes), that evaluates to something that is falsy, so that's why it stops executing.



If you want to iterate over each value, I'd advise a foreach loop instead.



foreach ($taxes as $key => $value) {
Log::debug($key);
// $this->saveTaxes($key, $value, $add);
}





share|improve this answer





















  • but if i want to take the key name?
    – SkullFire
    Nov 19 '18 at 15:28






  • 3




    @SkullFire You can access the key by using $key from Chris' example.
    – Dirk Scholten
    Nov 19 '18 at 15:29
















5














null is a falsy value, so when you do $t = current($taxes), that evaluates to something that is falsy, so that's why it stops executing.



If you want to iterate over each value, I'd advise a foreach loop instead.



foreach ($taxes as $key => $value) {
Log::debug($key);
// $this->saveTaxes($key, $value, $add);
}





share|improve this answer





















  • but if i want to take the key name?
    – SkullFire
    Nov 19 '18 at 15:28






  • 3




    @SkullFire You can access the key by using $key from Chris' example.
    – Dirk Scholten
    Nov 19 '18 at 15:29














5












5








5






null is a falsy value, so when you do $t = current($taxes), that evaluates to something that is falsy, so that's why it stops executing.



If you want to iterate over each value, I'd advise a foreach loop instead.



foreach ($taxes as $key => $value) {
Log::debug($key);
// $this->saveTaxes($key, $value, $add);
}





share|improve this answer












null is a falsy value, so when you do $t = current($taxes), that evaluates to something that is falsy, so that's why it stops executing.



If you want to iterate over each value, I'd advise a foreach loop instead.



foreach ($taxes as $key => $value) {
Log::debug($key);
// $this->saveTaxes($key, $value, $add);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 15:26









Chris Forrence

8,83483853




8,83483853












  • but if i want to take the key name?
    – SkullFire
    Nov 19 '18 at 15:28






  • 3




    @SkullFire You can access the key by using $key from Chris' example.
    – Dirk Scholten
    Nov 19 '18 at 15:29


















  • but if i want to take the key name?
    – SkullFire
    Nov 19 '18 at 15:28






  • 3




    @SkullFire You can access the key by using $key from Chris' example.
    – Dirk Scholten
    Nov 19 '18 at 15:29
















but if i want to take the key name?
– SkullFire
Nov 19 '18 at 15:28




but if i want to take the key name?
– SkullFire
Nov 19 '18 at 15:28




3




3




@SkullFire You can access the key by using $key from Chris' example.
– Dirk Scholten
Nov 19 '18 at 15:29




@SkullFire You can access the key by using $key from Chris' example.
– Dirk Scholten
Nov 19 '18 at 15:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377733%2fphp-doesnt-catch-next-array-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory