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

前端 未结 2 1508
悲哀的现实
悲哀的现实 2020-12-09 12:55

Following is the output of my multidimensional array $csmap_data

Array
(
    [0] => Array
        (
            [cs_map_id] => 84
                 


        
相关标签:
2条回答
  • 2020-12-09 13:13

    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);
    
    0 讨论(0)
  • 2020-12-09 13:36
    <?
     foreach($csmap_data as $key => $csm)
     {
      $csmap_data[$key]['flag'] = 1;
     }
    

    That should do the trick.

    0 讨论(0)
提交回复
热议问题