我在Laravel工作。我有两个数组,我想在第一个数组中插入第二个数组作为值。例如,给定
function contextMenu(evt)
{
evt.preventDefault();
}
,我想生成等价的
$firstArray = [
'id' => 1,
'propery_name' => 'Test Property'
];
$secondArray = [
'id' => 2,
'user_name' => 'john'
];
我怎样才能做到这一点?
答案 0 :(得分:0)
$resultArray = $firstArray;
$resultArray['userData'] = $secondArray;
答案 1 :(得分:0)
试试这个:
$firstArray = ['id'=>1,'propery_name'=>'Test Property'];
$secondArray = ['id'=>2,'user_name'=>'john'];
$resultArray = ['property' => $firstArray, 'userData' => $secondArray];
你新创建的数组应该给你这个:
{{ $resultArray['userData']['user_name'] }} = John
{{ $resultArray['property']['propery_name'] }} = Test Property
答案 2 :(得分:0)
$firstArray['userData'] = $secondArray;
return $firstArray;
//result
$resultArray = [
'id' => 1,
'propery_name' => 'Test Property',
'userData' => [
'id' => 2,
'user_name' => 'john'
]
];
答案 3 :(得分:0)
您可以使用Laravel集合类。推送功能是在数组中添加值的最佳方式。 https://laravel.com/docs/5.5/collections#method-put
在你的情况下,
$firstArray = [
'id' => 1,
'propery_name' => 'Test Property'
];
$secondArray = [
'id' => 2,
'user_name' => 'john'
];
$collection = collect($firstArray);
$collection->put('userData', $secondArray);
$collection->all();
输出:
['id' => 1,
'propery_name' => 'Test Property',
'userData' => [
'id' => 2,
'user_name' => 'john'
]
];