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;








-1















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









share|improve this question






















  • 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

















-1















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









share|improve this question






















  • 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













-1












-1








-1


1






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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer

























  • thank you for your help

    – Berkay Kara
    Mar 25 at 2:26











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%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









0














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






share|improve this answer

























  • thank you for your help

    – Berkay Kara
    Mar 25 at 2:26















0














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






share|improve this answer

























  • thank you for your help

    – Berkay Kara
    Mar 25 at 2:26













0












0








0







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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



















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.




draft saved


draft discarded














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





















































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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript