I want to move value of one array to another with specific key for example following is my first array.
firstArray
(
[0] => Array
(
[id] => 863
[flag] => 2
[qty] => 2
[size] => 8
)
[1] => Array
(
[id] => 861
[flag] => 2
[qty] => 2
[size] => 8
)
)
And following is my second array
TwoArray
(
[863] => Bead Headed Prince Nymphs size 18
[861] => Bead Headed Prince Nymphs size 14
)
And i want array with combined values like
mainarray(
[0] => Array
(
[id] => 863
[flag] => 2
[qty] => 2
[size] => 8
[name] =>Bead Headed Prince Nymphs size 18
)
[1] => Array
(
[id] => 861
[flag] => 2
[qty] => 2
[size] => 8
[name] => Bead Headed Prince Nymphs size 14
)
)
I used following code but i could not get output that i want.
$getResult = $wpdb->get_results("SELECT *FROM tablename");
if(isset($getResult) && !empty($getResult)){
foreach($getResult as $val){
$data = $val->fliesfromuser;
$fliesFromStore = json_decode($val->fliesid_in_store,true);
@$result = call_user_func_array('array_merge', $fliesFromStore);
echo "<pre>first";
print_r ($fliesFromStore);
echo "</pre>";
$postIds = array_column($fliesFromStore, 'id');
$args = array(
'post__in' =>$postIds,
'post_type' =>'product'
);
$getStorePosts = get_posts($args);
$postTitle = array_column($getStorePosts, 'post_title','ID');
echo "<pre>Two";
print_r ($postTitle);
echo "</pre>";
$test = array_merge($fliesFromStore,$postTitle);
echo "<pre>";
print_r ($test);
echo "</pre>";
die();
}
}
If someone know what I am doing wrong in my code then please help me on this .
If you make the first array associative then it's an easy foreach loop that is needed.
You can make firstarray associative by using array_column.
// Make firstarray associative
$firstarray = array_column($firstarray, NULL,"id");
foreach($twoarray as $key => $val){
$firstarray[$key]['name'] = $val;
}
var_dump($firstarray);
https://3v4l.org/Is3EC
Mind that firstarray is associative, to remove the association you can use array_values if you need to.
Edit: keep in mind this code assumed the keys in the second array exist in the first.
If it does not exist, it will create a new item in the first array with only 'name'.
You can use isset() to first make sure the $key exist in firstarray.
foreach($twoarray as $key => $val){
if(isset($firstarray[$key])) $firstarray[$key]['name'] = $val;
}
A slightly different version to @Andreas's answer:
It still uses a quick foreach loop to merge the correct array values together, just without using the associative array.
$fliesFromStore = array(
array(
'id' => 863,
'flag' => 2,
'qty' => 2,
'size' => 8
),
array(
'id' => 861,
'flag' => 2,
'qty' => 2,
'size' => 8
),
);
$postTitles = array(
863 => 'Bead Headed Prince Nymphs size 18',
861 => 'Bead Headed Prince Nymphs size 14'
);
foreach ($fliesFromStore as $key => $fly) {
$fliesFromStore[$key]['name'] = $postTitles[$fly['id']];
}
var_dump($fliesFromStore);
This will output:
Array
(
[0] => Array
(
[id] => 863
[flag] => 2
[qty] => 2
[size] => 8
[name] => Bead Headed Prince Nymphs size 18
)
[1] => Array
(
[id] => 861
[flag] => 2
[qty] => 2
[size] => 8
[name] => Bead Headed Prince Nymphs size 14
)
)
Here's a running example: https://eval.in/1035247
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With