【问题标题】:How to add an element into subarray in php [duplicate]如何在php中将元素添加到子数组中[重复]
【发布时间】:2020-05-09 12:39:29
【问题描述】:

我有一个数组:

$main_arr = [
  0=>[
         "el 01",
         "el 02",
         "el 03",
     ],
  1=>[
         "el 11",
         "el 12",
         "el 13",
     ],
  2=>[
         "el 21",
         "el 22",
         "el 23",
     ],
];

php 中是否有像ninja_array_method($main_arr, "el new") 这样的函数或单个语句将新元素添加到所有子数组中:

$main_arr = [
  0=>[
         "el 01",
         "el 02",
         "el 03",
         "el new",
     ],
  1=>[
         "el 11",
         "el 12",
         "el 13",
         "el new",
     ],
  2=>[
         "el 21",
         "el 22",
         "el 23",
         "el new",
     ],
];

我只想将这些元素“el new”添加到所有数组中,而不需要在一行中进行任何更改,或者只是像 add_el($main_arr, "el new") 这样的方法

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:

    如果您想要单线,这里有几个选项:

    到位

    $value = 'el new';
    array_walk($main_arr, function (&$entry) use ($value) { $entry[] = $value; });
    

    不可变的初始数组

    $value = 'el new';
    $modified = array_map(function ($entry) use ($value) { return array_merge($entry, [$value]); }, $main_arr);
    

    通过一些 array_map 技巧实现不可变

    $value = 'el new';
    $modified = array_map(function ($entry, $value) { return array_merge($entry, [$value]); }, $main_arr, array_fill(0, count($main_arr), $value));
    

    天空是极限

    【讨论】:

      【解决方案2】:

      使用foreach loopreference &

      foreach($data as $ind=>&$ar){
           $ar[] = 'el new';
      }
      

      Demo

      引用 & 改变您的子数组。

      如果你需要一个函数:

      function add_el($data,$elem){
          foreach($data as $ind=>&$ar){
               $ar[] = $elem;
          }
          return $data;
      }
      

      Demo

      【讨论】:

        【解决方案3】:

        如果你熟悉 laravel 集合

        你可以使用push方法

        $main_arr = [
            0=>[
                   "el 01",
                   "el 02",
                   "el 03",
               ],
            1=>[
                   "el 11",
                   "el 12",
                   "el 13",
               ],
            2=>[
                   "el 21",
                   "el 22",
                   "el 23",
               ],
          ];
        
          $expectedArray = [
            0=>[
                   "el 01",
                   "el 02",
                   "el 03",
                   "el new",
               ],
            1=>[
                   "el 11",
                   "el 12",
                   "el 13",
                   "el new",
               ],
            2=>[
                   "el 21",
                   "el 22",
                   "el 23",
                   "el new",
               ],
          ];
        
          $collection = collect($main_arr)
                            ->map(function($eachArray){
                            return collect($eachArray)->push('el new')->toArray();
                        })
                        ->toArray();
        
          dd($expectedArray,$collection);
        

        【讨论】:

        • 不,它只是 mappush。收藏有很多很多的功能
        猜你喜欢
        • 2021-03-10
        • 2012-06-18
        • 1970-01-01
        • 2011-12-02
        • 1970-01-01
        • 2014-05-26
        • 2020-08-08
        • 2018-06-28
        • 2013-01-12
        相关资源
        最近更新 更多