Combine associative arrays php - php

How do I merge these associative arrays so that the indices ([0],[1]) are preserved and var_id, name and id are merged? I've tried array_combine and array_merge_recursive without succes.
Input
Array (
[0] => Array (
[var_id] => 43
)
[1] => Array (
[var_id] => 25
)
)
Array (
[0] => Array (
[name] => Tortoise
)
[1] => Array (
[name] => Black
)
)
Array (
[0] => Array (
[id] => 1907
)
[1] => Array (
[id] => 1908
)
)
Desired output
Array (
[0] => Array (
[var_id] => 43
[name] => Tortoise
[id] => 1907
)
[1] => Array (
[var_id] => 25
[name] => Black
[id] => 1908
)
)
Cheers,
Adnan

Assuming your three arrays are called $array1, $array2, and $array3 here's a loop that will do what you want:
foreach(array($array1, $array2, $array3) AS $array) {
foreach($array AS $key => $value) {
foreach($value AS $subkey => $subvalue) {
$final[$key][$subkey] = $subvalue;
}
}
}
Working example: http://3v4l.org/GY9oa
If you have an unknown number of input arrays to merge, it would be trivial to turn this into a function to handle that.

Related

Combine/Merge arrays that have the same keys PHP

I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array.
I have an array of this type;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
)
)
Array
(
[Shoes] => Array
(
[FUBU] => Array
(
[0] => size6
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => White
)
)
)
I would like an output of the following kind;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have tried array_merge, array_merge_recursive but all are not working.
foreach ($county as $value) { print_r(array_merge($value)); } //$value contains the array
foreach ($county as $value) { print_r(array_merge_recursive($value)); } //$value contains the array
Kindly assist if you have an idea on how to solve this in PHP.
You could do it with a few nested array_walk() calls:
$arr=array(array("Shoes" => array("POLO" => array("Size5"))),
array("Shoes" => array("FUBU" => array("size6"))),
array("Bag" => array("HPBAG" => array("Black"))),
array("Bag" => array("HPBAG" => array("White"))));
$res=[];
array_walk($arr,function($a) use (&$res){
array_walk($a, function($ar,$type) use (&$res){
array_walk($ar, function ($arr,$brand) use (&$res,$type){
$res[$type][$brand]=array_merge($res[$type][$brand]??[],$arr);
});
});
});
print_r($res);
See the demo here: https://rextester.com/RFLQ18142
It produces this result:
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)

How to merge two array where one array is dynamic nested values

I'm having two arrays like this
$whole_orders
Array
(
[2] => Array
(
[0] => Array
(
[id] => 3
[food_id] => 1
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[food_id] => 2
)
[1] => Array
(
[id] => 1
[food_id] => 1
)
)
)
And $array
Array
(
[2] => Array
(
[0] => Array
(
[count] => 1
[subtotal] => 103.42
[tax] => 18.42
)
)
[1] => Array
(
[0] => Array
(
[count] => 2
[subtotal] => 303.42
[tax] => 38.42
)
)
)
Here I'm having two arrays such as $whole_orders & $array from which I need to merge the $array values into the $whole_orders..
And the $whole_orders having nested values which are dynamic..
Finally My array should be like this..
Array
(
[2] => Array
(
[0] => Array
(
[id] => 3
[food_id] => 1
)
[1] => Array
(
[count] => 1
[subtotal] => 103.42
[tax] => 18.42
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[food_id] => 2
)
[1] => Array
(
[id] => 1
[food_id] => 1
)
[2] => Array
(
[count] => 2
[subtotal] => 303.42
[tax] => 38.42
)
)
)
It should append nested values of $whole_orders array's..
If you think that my title is not correct please change it..
Thanks in advance..
Use foreach and iterate your $array and assigned to $whole_orders
<?php
// if $array is always single dimension array
foreach($array as $array_key=>$array_val)
{
$whole_orders [$array_key][]=$val[0];
}
// or if $array is multi dimension array
foreach($array as $array_key=>$array_val)
{
foreach($array_val as $key=>$val)
{
$whole_orders [$array_key][]=$val;
}
}
?>
Just do this it will achieve you desire output ,but when if the count is same for both array
foreach($array as $arrayKey => $arrayValue){
foreach($arrayValue as $key => $value){
$whole_orders[$arrayKey][] = $value;
}
}
print_r($whole_orders);
You can try below approach..
$arr3 = array();
foreach($arr1 as $key => $value) :
$arr3[$key] = $value;
if(isset($arr2[$key])) :
foreach($arr2[$key] as $k=>$val) :
$arr3[$key][] = $val;
endforeach;
endif;
endforeach;
print_r($arr3);

How can I flip an array when each value is also an array?

How would I go about flipping an array and establishing relationships between all the values and keys? For example:
I am trying to turn this:
Array (
[11913] => Array (
[0] => 4242
[1] => 3981
)
[9878] => Array (
[0] => 2901
[1] => 3981
)
[11506] => Array (
[0] => 3981
[1] => 2901
)
)
Into this:
Array (
[3981] => Array (
[0] => 11506
[1] => 9878
[2] => 11913
)
[2901] => Array (
[0] => 11506
[1] => 9878
)
[4242] => Array (
[0] => 11913
)
)
Are there any PHP functions that will already do this automatically? If not what would be a way of going about this? Can't seem to wrap my head around it.
Here you go.
$final_array = array();
foreach($initial_array as $key => $val){
foreach($val as $v){
$final_array[$v][] = $key;
}
}

Merge two arrays without replacing values

Im a bit lost or mind is not working as it should. I read other questions around but can get mine to work.
I got this array:
Array
(
[89] => Array
(
[0] => 16
[1] => 2
)
)
And i got this :
Array
(
[84] => Array
(
[0] => 2
)
[83] => Array
(
[0] => 2
)
[87] => Array
(
[0] => 2
[1] => 3
)
[88] => Array
(
[0] => 2
)
[89] => Array
(
[0] => 2
)
[90] => Array
(
[0] => 2
)
)
I should get all results but on key 89 i should get the value from first array.
Array
(
[84] => Array
(
[0] => 2
)
[83] => Array
(
[0] => 2
)
[87] => Array
(
[0] => 2
[1] => 3
)
[88] => Array
(
[0] => 2
)
[89] => Array
(
[0] => 16
[1] => 2
)
[90] => Array
(
[0] => 2
)
)
Array merge wont work :( .
Also after i get the result if the first array its :
Array
(
[89] => Array
(
[1] => 2
)
)
The resulting array should update to one record.
Im sure its a 1 min code for you gurus but arrays always been a pain for me.
Thanks
UPDATE : if i use array_merge_recursive it wont keep my keys :
print_r(array_merge_recursive($array1,$array2));
Array
(
[0] => Array
(
[0] => 16
[1] => 2
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 2
)
[3] => Array
(
[0] => 2
[1] => 3
)
[4] => Array
(
[0] => 2
)
[5] => Array
(
[0] => 2
)
[6] => Array
(
[0] => 2
)
)
array_merge_recursive should do the job; look at the documentation for more pointers: http://www.php.net/manual/en/function.array-merge-recursive.php
EDIT
The function behaved differently than I initially thought, hereĀ“s a different version of the function to solve your problem:
function array_merge_recursive_distinct(array &$array1, array &$array2) {
$merged = $array1;
foreach($array2 as $key => &$value) {
if(is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
Thanks to the community of php.net http://www.php.net/manual/en/function.array-merge-recursive.php#92195
If I understand it correctly, adding arrays should suffice:
$result_array = $array1 + $array2;
In contrast to array_merge, it won't overwrite values in first array and won't renumber numerical keys.
$newarray=array();
foreach(array_merge($array1,$array2) as $k=>$arr){
$newarray[$k]=array_merge($array1[$k],$array2[$k]);
}
foreach($firstArr as $key=>$val){
if(in_array($key,$secondArray)){
$secondArray[$key] = $firstArr[$key];
}
}
Try this may help you.

php re-sorting an array

I have an array of entries that are separated by year and month like below. I need to sort this array so that entries are "grouped" by year and then by category.
Array
(
[Oct 2011] => Array
(
[0] => Array
(
[title] => CAD Drawing Updates
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
[Sep 2011] => Array
(
[0] => Array
(
[title] => title
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
[1] => Array
(
[title] => edges
[file] => /gm-June2010-driver.pdf
[category] =>Walling
)
[2] => Array
(
[title] => Specification update
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
)
So this is the sort of thing I'm after.
Array
(
[Oct 2011] => Array
(
[Windows] => Array
(
[0] => Array
(
[title] => CAD Drawing Updates
[file] => /gm-June2010-driver.pdf
)
)
)
[Sep 2011] => Array
(
[Windows] => Array
(
[0] => Array
(
[title] => title
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
[1] => Array
(
[title] => Specification update
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
[Walling] => Array
(
[0] => Array
(
[title] => edges
[file] => /gm-June2010-driver.pdf
[category] => Curtain Walling
)
)
)
)
I'm not sure if this is a job for the sort functions, any help would be appreciated, thanks.
No, that can't be done with the sort function, you need to create a new array by iterating through your original array with nested foreach loops.
$newArr = array();
foreach($arr as $month => items) {
foreach($items as $data) {
$newArr[$month][$data["category"]][] = $data;
}
}
You don't want to sort but just to rename keys. PHP Arrays do not support to rename keys directly, so you need to create a new array first that is build with the new keys while maintaining the existing values.
At the end you can replace the original array with the new one:
$rekeyed = array();
foreach($array as $monthYear => &$value)
{
$r = sscanf($monthYear, '%s %d', $month, $year);
if ($r != 2) throw new Exception(sprintf('Invalid Key "%s".', $monthYear));
$categories = array();
foreach($value as &$item)
{
$category =& $item['category'];
unset($item['category']);
$categories[$category][] =& $item;
}
unset($item);
$value =& $categories;
unset($categories);
$rekeyed[$year][$month] =& $value;
}
unset($value);
$array =& $rekeyed;
unset($rekeyed);
print_r($array);
Output:
Array
(
[2011] => Array
(
[Oct] => Array
(
[Windows] => Array
(
[0] => Array
(
[title] => CAD Drawing Updates
[file] => /gm-June2010-driver.pdf
)
)
)
[Sep] => Array
(
[Windows] => Array
(
[0] => Array
(
[title] => title
[file] => /gm-June2010-driver.pdf
)
)
[Walling] => Array
(
[0] => Array
(
[title] => edges
[file] => /gm-June2010-driver.pdf
)
)
)
)
)
Demo
I think the function you should be looking at is uasort.
http://www.php.net/manual/en/function.uasort.php
First do a sort on the main array, the for each element in the array, do a usort on the child array.

Categories