Php doesn't catch next array element
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
add a comment |
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
6
BecauseNULL
is consideredfalsy
and your iterations stops.
– u_mulder
Nov 19 '18 at 15:24
1
Any reason to not just use aforeach
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
add a comment |
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
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
php laravel
edited Nov 19 '18 at 20:53


Salman Zafar
8571619
8571619
asked Nov 19 '18 at 15:22
SkullFire
275
275
6
BecauseNULL
is consideredfalsy
and your iterations stops.
– u_mulder
Nov 19 '18 at 15:24
1
Any reason to not just use aforeach
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
add a comment |
6
BecauseNULL
is consideredfalsy
and your iterations stops.
– u_mulder
Nov 19 '18 at 15:24
1
Any reason to not just use aforeach
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
add a comment |
1 Answer
1
active
oldest
votes
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);
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
}
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
add a comment |
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);
}
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
add a comment |
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);
}
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);
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
6
Because
NULL
is consideredfalsy
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