Array(
Array(
[name] => John
[last] => Smith
[status] => sent
)
Array(
[name] => Jane
[last] => Doe
[status] => pending
)
Array(
[name] => Kripky
[last] => Woe
[status] => pending
)
)
代码:
$arr = array();
foreach ($res as $key => $value) {
if($value['status'] == 'sent'){
$arr[] = array($value, $value['dupe'] = 1);
}
}
如果阵列上有一个$value['dupe'] = 1
,如何在所有阵列上添加status = sent
预期结果:
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
)
)
答案 0 :(得分:1)
这个循环应该为你做到:
foreach ($a as $i) {
if ($i['status'] == 'sent') {
foreach ($a as &$_i) {
$_i['dupe'] = 1;
}
}
}
注意:这通过引用(&
)使用变量,因此它将更新实际数组。
答案 1 :(得分:0)
此代码可以帮助您
// 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