如何在laravel中合并一个数组元素?

时间:2018-11-16 06:31:27

标签: php arrays laravel-5

我是新来的laravel。我想在一个数组中合并2个多维数组。我正在使用array_merge函数,但不起作用。我想合并array [0]位置值和array [1]位置值。

这是我当前的数组,像这样

Array
(
  [0] => Illuminate\Support\Collection Object
      (
          [items:protected] => Array
              (
                  [0] => stdClass Object
                      (
                          [product_id] => 2
                          [sale_datetime] => 2018-10-15 16:33:59
                          [name] => Tea
                          [totalqty] => 3
                      )
                  [1] => stdClass Object
                      (
                          [product_id] => 2
                          [sale_datetime] => 2018-10-16 10:44:14
                          [name] => Tea
                          [totalqty] => 5
                      )
                 )
      )
  [1] => Illuminate\Support\Collection Object
      (
          [items:protected] => Array
              (
                  [0] => stdClass Object
                      (
                          [product_id] => 3
                          [sale_datetime] => 2018-11-15 18:04:36
                          [name] => Coffee
                          [totalqty] => 20
                      )
              )
      )
)

我想使数组像下面的数组

Array
(
  [0] => Illuminate\Support\Collection Object
      (
          [items:protected] => Array
              (
                  [0] => stdClass Object
                      (
                          [product_id] => 2
                          [sale_datetime] => 2018-10-15 16:33:59
                          [name] => Tea
                          [totalqty] => 3
                      )

                  [1] => stdClass Object
                      (
                          [product_id] => 2
                          [sale_datetime] => 2018-10-16 10:44:14
                          [name] => Tea
                          [totalqty] => 5
                      )
                  [2] => stdClass Object
                      (
                          [product_id] => 3
                          [sale_datetime] => 2018-11-15 18:04:36
                          [name] => Coffee
                          [totalqty] => 20
                      )
                 )
      )
)

3 个答案:

答案 0 :(得分:0)

您的代码输出是不是直接数组的对象,请使用foreach循环,然后使用array_merge。

例如。 假设您的输出存储在变量$ array

foreach($array as $arrays){
  array_merge('$arrays', $array_you_want_to_merge);
}

注意:-您的代码结构不清楚,因此可能无法正常工作,但会给您一个想法。

答案 1 :(得分:0)

尝试此代码

$a = $arr[0];
$b = $arr[1];
$newArr= array();
$newArr["items:protected"] = array_merge($a["items:protected"],$b["items:protected"]);

答案 2 :(得分:0)

您正在使用laravel中的收藏集。虽然您可以使用默认的PHP函数,例如array_merge,但为什么不通过使用merge使用laravel方式。

从文档中复制并粘贴:

$collection = collect(['product_id' => 1, 'price' => 100]);

$merged = $collection->merge(['price' => 200, 'discount' => false]);

$merged->all();

// ['product_id' => 1, 'price' => 200, 'discount' => false]

或者如果您希望合并2个收藏夹,则:

$original = new Collection(['foo']);

$latest = new Collection(['bar']);

$merged = $original->merge($latest); // Contains foo and bar.

取自here