First array:
Array (
[0] => Array ( [id] => 1 [occ] => 14 )
[1] => Array ( [id] => 2 [occ] => 12 )
[2] => Array ( [id] => 4 [occ] => 2 )
)
Second array:
Array (
[0] => Array ( [id] => 1 [company_name] => Google [CEO] => Mike )
[1] => Array ( [id] => 2 [company_name] => Apple [CEO] => Jones)
[2] => Array ( [id] => 2 [company_name] => Bmw [CEO] => Steve)
[3] => Array ( [id] => 3 [company_name] => Hardsoft [CEO] => Lucy )
[4] => Array ( [id] => 4 [company_name] => Lays [CEO] => Morty)
)
I Would like to merge these arrays to something like that:
Array (
[0] => Array ( [id] => 1 [company_name] => Google [CEO] => Mike [occ] => 14)
[1] => Array ( [id] => 2 [company_name] => Apple [CEO] => Jones [occ] => 12)
[2] => Array ( [id] => 3 [company_name] => Bmw [CEO] => Steve [occ] => 0)
[3] => Array ( [id] => 4 [company_name] => Hardsoft [CEO] => Lucy [occ] => 2)
[4] => Array ( [id] => 5 [company_name] => Lays [CEO] => Morty [occ] => 0)
)
Then sort them by occ so Google will be first, Apple second, Hardsoft third and so on.
How can I archive it?
This should work for you:
First of I use array_column()
to get an array with the id's
from the first array as "lookup table". Then I use array_values()
to reindex the second array.
After this I start with looping through the second array and reassigning the id, by simply using the key from the innerArray + 1. So that you don't have duplicate id's.
Then I look with array_search()
if the new id is in the lookup table and if yes I assign the value occ
from the first array to the second array. If not I simply assign 0 as value of the key occ
in the second array.
After that is all done, it is a simple sort with usort()
to compare the values of occ
and sort it by it's value (Note: If you want to change the sorting from DESC
to ASC
just simply change <
to >
in the usort()
call).
<?php
$ids = array_column($arr1, "id");
$arr2 = array_values($arr2);
foreach($arr2 as $k => &$v) {
$v["id"] = ($k+1);
if(in_array($v["id"], $ids))
$arr2[$k]["occ"] = $arr1[array_search($v["id"], $ids)]["occ"];
else
$arr2[$k]["occ"] = 0;
}
usort($arr2, function($a, $b){
if($a["occ"] == $b["occ"])
return 0;
return $a["occ"] < $b["occ"] ? 1 : -1;
});
print_r($arr2);
?>
output:
Array
(
[0] => Array
(
[id] => 1
[company_name] => Google
[CEO] => Mike
[occ] => 14
)
//...
[4] => Array
(
[id] => 3
[company_name] => Bmw
[CEO] => Steve
[occ] => 0
)
)
first change your first array to an associative one where the ids are keys - so the most basic way would just be a simple loop
$lookup_array = [];
foreach ($firstarray as $row) {
$lookup_array[$row['id']] = $row['occ'];
}
then you can just loop through the second array and add what you need
foreach ($second_array as &$row) {
$id = $row['id'];
$occ = isset($lookup_array[$id]) ? $lookup_array[$id] : 0;
$row['occ'] = $occ;
}
notice the & pointer to make $row a reference to the array element.
good luck
You can do it via iteration something like this
$first_array=Array (
[0] => Array ( [id] => 1 [occ] => 14 )
[1] => Array ( [id] => 2 [occ] => 12 )
[2] => Array ( [id] => 4 [occ] => 2 )
);
$second_array=Array (
[0] => Array ( [id] => 1 [company_name] => Google [CEO] => Mike )
[1] => Array ( [id] => 2 [company_name] => Apple [CEO] => Jones)
[2] => Array ( [id] => 2 [company_name] => Bmw [CEO] => Steve)
[3] => Array ( [id] => 3 [company_name] => Hardsoft [CEO] => Lucy )
[4] => Array ( [id] => 4 [company_name] => Lays [CEO] => Morty)
)
$third_array=$second_array;
for($i=0;$i<sizeof($second_array);$i++)
{
for($j=0;$j<sizeof($first_array);$j++)
{
if($second_array[$i][id]==$first_array[$j][id])
{
$third_array[$i][occ]=$first_array[$j][occ];
}
}
if(!isset($third_array[$i][occ]))
{
$third_array[$i][occ]=0;
}
}
now check it via debugging
var_dump($third_array);
Probably you will see something like this
Array (
[0] => Array ( [id] => 1 [company_name] => Google [CEO] => Mike [occ] => 14)
[1] => Array ( [id] => 2 [company_name] => Apple [CEO] => Jones [occ] => 12)
[2] => Array ( [id] => 3 [company_name] => Bmw [CEO] => Steve [occ] => 0)
[3] => Array ( [id] => 4 [company_name] => Hardsoft [CEO] => Lucy [occ] => 2)
[4] => Array ( [id] => 5 [company_name] => Lays [CEO] => Morty [occ] => 0)
)
I hope this will help you
Here is the complete one to all solution with sorting as well :)
$a = array (
0 => array ( 'id' => 1, 'occ' => 14 ),
1 => array ( 'id' => 2, 'occ' => 12 ),
2 => array ( 'id' => 4, 'occ' => 2 ),
);
$b = array (
'0' => array ( 'id' => 1, 'company_name' => 'Google', 'CEO' => 'Mike' ),
'1' => array ( 'id' => 2, 'company_name' => 'Apple', 'CEO' => 'Jones'),
'2' => array ( 'id' => 2, 'company_name' => 'Bmw', 'CEO' => 'Steve'),
'3' => array ( 'id' => 3, 'company_name' => 'Hardsoft', 'CEO' => 'Lucy' ),
'4' => array ( 'id' => 4, 'company_name' => 'Lays', 'CEO' => 'Morty'),
);
$result = [];
foreach($b as $b_key => $b_val) {
$merge_array = getSubArrayOnId($b_val['id'], $a);//get portion to be merged from $a
if(!empty($merge_array))
$result[] = $b_val + $merge_array;
else
$result[] = $b_val + ['occ' => 0];
}
//now sorting using user defined function
usort($result, 'cmpOcc');
function getSubArrayOnId($id, $array)
{
foreach ($array as $key=>$val) {
if($val['id'] == $id) {
return $array[$key];//return the actual sub-array
}
}
return [];//if nothing found return empty array
}
function cmpOcc($a, $b) {
return $b['occ'] - $a['occ'];
}
Now using print_r we will get the following result.
echo "<pre>";
print_r($result);
Output (Sorted):
Array
(
[0] => Array
(
[id] => 1
[company_name] => Google
[CEO] => Mike
[occ] => 14
)
[1] => Array
(
[id] => 2
[company_name] => Bmw
[CEO] => Steve
[occ] => 12
)
[2] => Array
(
[id] => 2
[company_name] => Apple
[CEO] => Jones
[occ] => 12
)
[3] => Array
(
[id] => 4
[company_name] => Lays
[CEO] => Morty
[occ] => 2
)
[4] => Array
(
[id] => 3
[company_name] => Hardsoft
[CEO] => Lucy
[occ] => 0
)
)