Answer the question
In order to leave comments, you need to log in
How to fill array key with values of another php array?
Good afternoon, please tell me how to merge two arrays into one?
First array:
$allSpecCat = [];
foreach($specsCatId as $sci)
{
$allSpecCat = [
'blog_cat_id' => $sci->id,
];
}
$data = [
'name' => $name,
'guid' => $idspec,
'blog_cat_id' => '',
'created_at' => date("Y-m-d H:i:s"),
'updated_at' => date("Y-m-d H:i:s"),
];
$res = array_merge($data, $allSpecCat);
var_dump($res);
Answer the question
In order to leave comments, you need to log in
data['blog_cat_id']
remains empty because the array is $data
one-dimensional, and $allSpecCat
- two-dimensional.
array_merge
does not find the corresponding keys from the array $data
in the array $allSpecCat
, and therefore adds $data
to the resulting array unchanged. Then the values from the array $allSpecCat
with numerical indices are added to the end of the resulting array.
So it's clear that I want to transfer the values from $allSpecCat['blog_cat_id'] to $data['blog_cat_id'].
# если $allSpecCat - двумерный массив и нужно получить все значения из него
$data['blog_cat_id'] = array_map(function($v) {
return $v['blog_cat_id'];
}, $allSpecCat);
# если $allSpecCat - ассоциативный массив
$data['blog_cat_id'] = $allSpecCat['blog_cat_id']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question