gpt4 book ai didi

php - 在没有for循环的情况下在php中组合数组

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:46 26 4
gpt4 key购买 nike

我有两个数组首先是

Array
(
[0] => Array
(
[image] => Copy1vs.jpg
[title] => V.S Achuthanandhan
[groupId] => 1
[masterId] => 1
[id] => 1
[itemId] => 1
[status] => 1

)

[1] => Array
(
[image] => Copy1pinarayi.jpg
[title] => Pinarayi Vijayan
[groupId] => 1
[masterId] => 2
[id] => 2
[itemId] => 2
[status] => 1

)

[2] => Array
(
[image] => Copy1chandy.jpg
[title] => Oommen Chandy
[groupId] => 1
[masterId] => 3
[id] => 3
[itemId] => 3
[status] => 1

)
)

第二个是

Array
(
[0] => Array
(
[image] => Copy1antony.jpg
[title] => A. K. Antony
[groupId] => 1
[masterId] => 4
[id] => 4
[itemId] => 4
[status] => 1

)

)

如何将这两个数组合并为一个数组喜欢

Array
(
[0] => Array
(
[image] => Copy1vs.jpg
[title] => V.S Achuthanandhan
[groupId] => 1
[masterId] => 1
[id] => 1
[itemId] => 1
[status] => 1

)

[1] => Array
(
[image] => Copy1pinarayi.jpg
[title] => Pinarayi Vijayan
[groupId] => 1
[masterId] => 2
[id] => 2
[itemId] => 2
[status] => 1

)

[2] => Array
(
[image] => Copy1chandy.jpg
[title] => Oommen Chandy
[groupId] => 1
[masterId] => 3
[id] => 3
[itemId] => 3
[status] => 1

)

[3] => Array
(
[image] => Copy1antony.jpg
[title] => A. K. Antony
[groupId] => 1
[masterId] => 4
[id] => 4
[itemId] => 4
[status] => 1

)
)

我尝试了 array_merge 方法但没有按照我的要求工作是否可以不使用 for 循环..?这些数组从数据库中获取为

$itemListArray = array();
foreach($subcat as $sc){
$itemList = DB::table('votemasteritems')
->leftjoin('votemaster','votemaster.id','=','votemasteritems.masterId')
->leftjoin('items','items.id','=','votemasteritems.itemId')
->leftjoin('category','category.id','=','items.categoryId')
->select('items.image','votemaster.title','votemaster.groupId','votemaster.id as masterId','votemasteritems.*')
->where('votemaster.groupId',1)
->where('category.Id',$sc->id)
->get();

array_merge($itemListArray, $itemList);

}

最佳答案

是的,PHP 有array_merge() 函数:http://php.net/array_merge

$combinedArray = array_merge($array1, $array2); 一样使用它

关于php - 在没有for循环的情况下在php中组合数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com