Foreach multi-state controlHow can I get useful error messages in PHP?Invalid argument supplied for foreach()How to Sort Multi-dimensional Array by Value?Reference — What does this symbol mean in PHP?“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHPHow does PHP 'foreach' actually work?Reference - What does this error mean in PHP?Laravel blade check empty foreachmulti array in an array using foreachHow to put div inside foreach loop?
How effective would a full set of plate armor be against wild animals found in temperate regions (bears, snakes, wolves)?
Purpose of cylindrical attachments on Power Transmission towers
Someone who is granted access to information but not expected to read it
Any gotchas in buying second-hand sanitary ware?
Why are backslashes included in this shell script?
Why did the Death Eaters wait to reopen the Chamber of Secrets?
What's the reason for the decade jump in the recent X-Men trilogy?
usage of mir gefallen
Should I worry about having my credit pulled multiple times while car shopping?
A flower's head or heart?
Is pointing finger in meeting consider bad?
Would a character with eternal youth be AL-compliant?
Must a CPU have a GPU if the motherboard provides a display port (when there isn't any separate video card)?
Dedicated bike GPS computer over smartphone
Why did Robert pick unworthy men for the White Cloaks?
Short story about psychologist analyzing demon
Why can't we feel the Earth's revolution?
Does every chapter have to "blow the reader away" so to speak?
Nth term of Van Eck Sequence
How to represent jealousy in a cute way?
Is it possible to install Firefox on Ubuntu with no desktop enviroment?
How can I find out about the game world without meta-influencing it?
New Site Design!
Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?
Foreach multi-state control
How can I get useful error messages in PHP?Invalid argument supplied for foreach()How to Sort Multi-dimensional Array by Value?Reference — What does this symbol mean in PHP?“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHPHow does PHP 'foreach' actually work?Reference - What does this error mean in PHP?Laravel blade check empty foreachmulti array in an array using foreachHow to put div inside foreach loop?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm listing data with Foreach and I'm trying to print it as 1 if all of this data is active and 1 is active, but there is a problem with the code
foreach($veri as $data)
if($data['status']==1)
echo 'active';
else if($data['status']==2)
echo 'not active';
Result:
1active1active1active2not active
trying to do
active or not active
php
|
show 1 more comment
I'm listing data with Foreach and I'm trying to print it as 1 if all of this data is active and 1 is active, but there is a problem with the code
foreach($veri as $data)
if($data['status']==1)
echo 'active';
else if($data['status']==2)
echo 'not active';
Result:
1active1active1active2not active
trying to do
active or not active
php
there's nothing wrong with your loop. i think the issue could be from the code before the loop runs
– Festus Yuma
Mar 25 at 1:44
1
There is literally no way the PHP code shown produces "1active1active1active2not active". Where do the numbers come from? Can you also please explain clearly what "print it as 1 if all of this data is active and 1 is active" means?
– Phil
Mar 25 at 1:45
can you add the code where you declared the array you're looping through
– Festus Yuma
Mar 25 at 1:45
@Phil I'm holding a json data in the database. There are a lot of data here and they have a situation. If the status of each data is 1 get active. If the status of each data is not 1 I am trying not to be not active.
– Berkay Kara
Mar 25 at 1:49
@FestusYuma I get the s status of the data that I have saved json in the database.
– Berkay Kara
Mar 25 at 1:54
|
show 1 more comment
I'm listing data with Foreach and I'm trying to print it as 1 if all of this data is active and 1 is active, but there is a problem with the code
foreach($veri as $data)
if($data['status']==1)
echo 'active';
else if($data['status']==2)
echo 'not active';
Result:
1active1active1active2not active
trying to do
active or not active
php
I'm listing data with Foreach and I'm trying to print it as 1 if all of this data is active and 1 is active, but there is a problem with the code
foreach($veri as $data)
if($data['status']==1)
echo 'active';
else if($data['status']==2)
echo 'not active';
Result:
1active1active1active2not active
trying to do
active or not active
php
php
asked Mar 25 at 1:38
Berkay KaraBerkay Kara
204
204
there's nothing wrong with your loop. i think the issue could be from the code before the loop runs
– Festus Yuma
Mar 25 at 1:44
1
There is literally no way the PHP code shown produces "1active1active1active2not active". Where do the numbers come from? Can you also please explain clearly what "print it as 1 if all of this data is active and 1 is active" means?
– Phil
Mar 25 at 1:45
can you add the code where you declared the array you're looping through
– Festus Yuma
Mar 25 at 1:45
@Phil I'm holding a json data in the database. There are a lot of data here and they have a situation. If the status of each data is 1 get active. If the status of each data is not 1 I am trying not to be not active.
– Berkay Kara
Mar 25 at 1:49
@FestusYuma I get the s status of the data that I have saved json in the database.
– Berkay Kara
Mar 25 at 1:54
|
show 1 more comment
there's nothing wrong with your loop. i think the issue could be from the code before the loop runs
– Festus Yuma
Mar 25 at 1:44
1
There is literally no way the PHP code shown produces "1active1active1active2not active". Where do the numbers come from? Can you also please explain clearly what "print it as 1 if all of this data is active and 1 is active" means?
– Phil
Mar 25 at 1:45
can you add the code where you declared the array you're looping through
– Festus Yuma
Mar 25 at 1:45
@Phil I'm holding a json data in the database. There are a lot of data here and they have a situation. If the status of each data is 1 get active. If the status of each data is not 1 I am trying not to be not active.
– Berkay Kara
Mar 25 at 1:49
@FestusYuma I get the s status of the data that I have saved json in the database.
– Berkay Kara
Mar 25 at 1:54
there's nothing wrong with your loop. i think the issue could be from the code before the loop runs
– Festus Yuma
Mar 25 at 1:44
there's nothing wrong with your loop. i think the issue could be from the code before the loop runs
– Festus Yuma
Mar 25 at 1:44
1
1
There is literally no way the PHP code shown produces "1active1active1active2not active". Where do the numbers come from? Can you also please explain clearly what "print it as 1 if all of this data is active and 1 is active" means?
– Phil
Mar 25 at 1:45
There is literally no way the PHP code shown produces "1active1active1active2not active". Where do the numbers come from? Can you also please explain clearly what "print it as 1 if all of this data is active and 1 is active" means?
– Phil
Mar 25 at 1:45
can you add the code where you declared the array you're looping through
– Festus Yuma
Mar 25 at 1:45
can you add the code where you declared the array you're looping through
– Festus Yuma
Mar 25 at 1:45
@Phil I'm holding a json data in the database. There are a lot of data here and they have a situation. If the status of each data is 1 get active. If the status of each data is not 1 I am trying not to be not active.
– Berkay Kara
Mar 25 at 1:49
@Phil I'm holding a json data in the database. There are a lot of data here and they have a situation. If the status of each data is 1 get active. If the status of each data is not 1 I am trying not to be not active.
– Berkay Kara
Mar 25 at 1:49
@FestusYuma I get the s status of the data that I have saved json in the database.
– Berkay Kara
Mar 25 at 1:54
@FestusYuma I get the s status of the data that I have saved json in the database.
– Berkay Kara
Mar 25 at 1:54
|
show 1 more comment
1 Answer
1
active
oldest
votes
Sounds like you want to only print "active" if all entries have status
equal to 1
and "not active" otherwise.
You can use array_reduce()
to reduce the array to a single boolean
$allActive = array_reduce($ver1, function($active, $data)
return $active && $data['status'] === 1;
, true);
echo $allActive ? 'active' : 'not active';
Demo ~ https://3v4l.org/5Ve9d
The above is somewhat inefficient as it will iterate over the entire array, even after it finds a value other than 1
.
This is slightly more verbose but optimally exits early
$allActive = true;
forEach($ver1 as $data)
if (!($allActive = $data['status'] === 1))
break;
Demo ~ https://3v4l.org/UDSpt
thank you for your help
– Berkay Kara
Mar 25 at 2:26
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%2f55330296%2fforeach-multi-state-control%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
Sounds like you want to only print "active" if all entries have status
equal to 1
and "not active" otherwise.
You can use array_reduce()
to reduce the array to a single boolean
$allActive = array_reduce($ver1, function($active, $data)
return $active && $data['status'] === 1;
, true);
echo $allActive ? 'active' : 'not active';
Demo ~ https://3v4l.org/5Ve9d
The above is somewhat inefficient as it will iterate over the entire array, even after it finds a value other than 1
.
This is slightly more verbose but optimally exits early
$allActive = true;
forEach($ver1 as $data)
if (!($allActive = $data['status'] === 1))
break;
Demo ~ https://3v4l.org/UDSpt
thank you for your help
– Berkay Kara
Mar 25 at 2:26
add a comment |
Sounds like you want to only print "active" if all entries have status
equal to 1
and "not active" otherwise.
You can use array_reduce()
to reduce the array to a single boolean
$allActive = array_reduce($ver1, function($active, $data)
return $active && $data['status'] === 1;
, true);
echo $allActive ? 'active' : 'not active';
Demo ~ https://3v4l.org/5Ve9d
The above is somewhat inefficient as it will iterate over the entire array, even after it finds a value other than 1
.
This is slightly more verbose but optimally exits early
$allActive = true;
forEach($ver1 as $data)
if (!($allActive = $data['status'] === 1))
break;
Demo ~ https://3v4l.org/UDSpt
thank you for your help
– Berkay Kara
Mar 25 at 2:26
add a comment |
Sounds like you want to only print "active" if all entries have status
equal to 1
and "not active" otherwise.
You can use array_reduce()
to reduce the array to a single boolean
$allActive = array_reduce($ver1, function($active, $data)
return $active && $data['status'] === 1;
, true);
echo $allActive ? 'active' : 'not active';
Demo ~ https://3v4l.org/5Ve9d
The above is somewhat inefficient as it will iterate over the entire array, even after it finds a value other than 1
.
This is slightly more verbose but optimally exits early
$allActive = true;
forEach($ver1 as $data)
if (!($allActive = $data['status'] === 1))
break;
Demo ~ https://3v4l.org/UDSpt
Sounds like you want to only print "active" if all entries have status
equal to 1
and "not active" otherwise.
You can use array_reduce()
to reduce the array to a single boolean
$allActive = array_reduce($ver1, function($active, $data)
return $active && $data['status'] === 1;
, true);
echo $allActive ? 'active' : 'not active';
Demo ~ https://3v4l.org/5Ve9d
The above is somewhat inefficient as it will iterate over the entire array, even after it finds a value other than 1
.
This is slightly more verbose but optimally exits early
$allActive = true;
forEach($ver1 as $data)
if (!($allActive = $data['status'] === 1))
break;
Demo ~ https://3v4l.org/UDSpt
edited Mar 25 at 2:06
answered Mar 25 at 1:55
PhilPhil
102k13152171
102k13152171
thank you for your help
– Berkay Kara
Mar 25 at 2:26
add a comment |
thank you for your help
– Berkay Kara
Mar 25 at 2:26
thank you for your help
– Berkay Kara
Mar 25 at 2:26
thank you for your help
– Berkay Kara
Mar 25 at 2:26
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.
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%2f55330296%2fforeach-multi-state-control%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
there's nothing wrong with your loop. i think the issue could be from the code before the loop runs
– Festus Yuma
Mar 25 at 1:44
1
There is literally no way the PHP code shown produces "1active1active1active2not active". Where do the numbers come from? Can you also please explain clearly what "print it as 1 if all of this data is active and 1 is active" means?
– Phil
Mar 25 at 1:45
can you add the code where you declared the array you're looping through
– Festus Yuma
Mar 25 at 1:45
@Phil I'm holding a json data in the database. There are a lot of data here and they have a situation. If the status of each data is 1 get active. If the status of each data is not 1 I am trying not to be not active.
– Berkay Kara
Mar 25 at 1:49
@FestusYuma I get the s status of the data that I have saved json in the database.
– Berkay Kara
Mar 25 at 1:54