我有这样的数组数据
我想在条件为expected_count
的情况下向数组添加新键if
,
if flags key == is_join ? expected_count=2
if flags key == is_grade ? expected_count=1
所以结果将是这样
如何做到最好?
答案 0 :(得分:2)
使用foreach遍历数组
foreach($data['sections'] as $key => $section){
if(array_key_exists('is_join', $section['flags'])){
$data['sections'][$key]['flags']['is_join']['expected_count'] = 2;
}
if(array_key_exists('is_grade', $section['flags'])){
$data['sections'][$key]['flags']['is_grade']['expected_count'] = 1;
}
}
我希望这会起作用。