How to insert a new key and value in multidimensional array?

好久不见. 提交于 2019-11-28 08:45:22
<?
 foreach($csmap_data as $key => $csm)
 {
  $csmap_data[$key]['flag'] = 1;
 }

That should do the trick.

You can also do it using php array functions

$csmap_data = array_map(function($arr){
    return $arr + ['flag' => 1];
}, $csmap_data);

UPDATE: to use multiple variables in callback function of array_map function we can do it by use

$flagValue = 1;
$csmap_data = array_map(function($arr){ use ($flagValue)
    return $arr + ['flag' => $flagValue];
}, $csmap_data);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!