Merge Multi Array Laravel

问题描述 投票:-1回答:2

我有3个数组,如下所示:

"promotion_news_id" => array:3 [▼
    0 => "ID1"
    1 => "ID2"
    2 => "ID3"
  ]
  "promotion_news_name" => array:3 [▼
    0 => "abc 1"
    1 => "abc 2"
    2 => "abc 3"
  ]
  "news_id" => array:3 [▼
    0 => "Title 1"
    1 => "Title 2
  ]

我想将这3个数组合并为1个数组,如下所示:

array:3 [▼
  0 => array:3 [▼
    'promotion_news_id' => "ID1"
    'promotion_news_name' => "abc 1"
    'news_id' => "Title 1"

  ]
  1 => array:3 [▼
    'promotion_news_id' => "ID2"
    'promotion_news_name' => "abc 2"
    'news_id' => "Title 2" 
  ],
  2 => array:3 [▼
    'promotion_news_id' => "ID3"
    'promotion_news_name' => "abc 3"
    'news_id' => "null"

  ]
]

我尝试过但未获得空值。任何帮助,不胜感激!非常感谢!

php arrays laravel multidimensional-array merge
2个回答
5
投票

假设所有三个数组都具有相同数量的元素,

$result = [];
foreach($promotion_news_id as $index => $value){
    $result[] = array(
        'promotion_news_id' => $value
        'promotion_news_name' => $promotion_news_name[$index]
        'news_id' => $news_id[$index]
    )
}

[此外,不确定为什么将空值设为"null"而不是null。前者只是一个字符串,而不是实际的空值。


0
投票

代替"null"null

文档here

© www.soinside.com 2019 - 2024. All rights reserved.