2
Array( Array( [name] => John [last] => Smith [status] => sent ) Array( [name] => Jane [last] => Doe [status] => pending ) Array( [name] => Kripky [last] => Woe [status] => pending ) ) 

Code:

 $arr = array(); foreach ($res as $key => $value) { if($value['status'] == 'sent'){ $arr[] = array($value, $value['dupe'] = 1); } } 

How to add $value['dupe'] = 1 on all arrays if there is one status = sent on an array

Expected result:

Array( Array( [name] => John [last] => Smith [status] => sent [dupe] => 1 ) Array( [name] => Jane [last] => Doe [status] => pending [dupe] => 1 ) Array( [name] => Kripky [last] => Woe [status] => pending [dupe] => 1 ) ) 

2 Answers 2

1

This loop should do it for you:

foreach ($a as $i) { if ($i['status'] == 'sent') { foreach ($a as &$_i) { $_i['dupe'] = 1; } } } 

Example Demo


Note: This uses variable by reference (&) so it will update the actual array.

Sign up to request clarification or add additional context in comments.

Comments

0

This code may help you

// first, define a function that tells if there is a statut = sent function statut_exist($arr) { foreach ($arr as $value) if($value['status'] == 'sent') return true; return false; } // then add the dupe if there is a statut = sent if(statut_exist()) foreach ($res as $value) $value['dupe'] = 1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.