How can I merge arrays with along an identifier? - php

I have two arrays like this:
$arr1 = ['yellow' => 1, 'red', => 5];
$arr2 = ['gray' => 3, 'black' => 8, 'orange' => 6, 'yellow' => 9];
And I want this result:
$merged = ['yellow' => [1, 'arr1'], 'red', => [5, 'arr1'], 'gray' => [3, 'arr2'], 'black' => [8, 'arr2'], 'orange' => [6, 'arr2'], 'yellow' => [9, 'arr2']];
But as I've read the documentation about array_merge(),it doesn't have such a option. Is there any function to do that for me?

This probably is what you are looking for:
<?php
$input = [
'arr1' => ['yellow' => 1, 'red' => 5],
'arr2' => ['yellow' => 10, 'gray' => 3, 'black' => 8, 'orange' => 6]
];
$output = [];
array_walk($input, function ($entry, $source) use (&$output) {
foreach ($entry as $key=>$val) {
$output[$key][] = [$val, $source];
}
});
print_r($output);
The output of above code obviously is:
Array
(
[yellow] => Array
(
[0] => Array
(
[0] => 1
[1] => arr1
)
[1] => Array
(
[0] => 10
[1] => arr2
)
)
[red] => Array
(
[0] => Array
(
[0] => 5
[1] => arr1
)
)
[gray] => Array
(
[0] => Array
(
[0] => 3
[1] => arr2
)
)
[black] => Array
(
[0] => Array
(
[0] => 8
[1] => arr2
)
)
[orange] => Array
(
[0] => Array
(
[0] => 6
[1] => arr2
)
)
)

Ok, it took me a bit longer to come up with this answer because you have a bug in your question. See the ',' behind 'red' in array 1.
$arr1 = ['yellow' => 1, 'red' => 5];
$arr2 = ['gray' => 3, 'black' => 8, 'yellow' => 3, 'orange' => 6];
$arrays = ['arr1','arr2'];
$merged = [];
foreach ($arrays as $array) {
foreach (${$array} as $color => $value) {
$merged[$color][] = [$value,$array];
}
}
echo '<pre>';
print_r($merged);
echo '</pre>';
To keep both values of 'yellow' I introduced an extra layer of arrays. I don't see how to keep them otherwise.
So the code was tested and returns:
Array
(
[yellow] => Array
(
[0] => Array
(
[0] => 1
[1] => arr1
)
[1] => Array
(
[0] => 3
[1] => arr2
)
)
[red] => Array
(
[0] => Array
(
[0] => 5
[1] => arr1
)
)
[gray] => Array
(
[0] => Array
(
[0] => 3
[1] => arr2
)
)
[black] => Array
(
[0] => Array
(
[0] => 8
[1] => arr2
)
)
[orange] => Array
(
[0] => Array
(
[0] => 6
[1] => arr2
)
)
)

Based on #arkascha answer a version that handle duplicates:
<?php
$input = [
'arr1'=> ['yellow' => 1,'red' => 5, 'black' => 4],
'arr2'=> ['gray' => 3,'yellow' => 3, 'black' => 8,'orange' => 6],
'arr3'=> ['orange' => 2]
];
$output = [];
array_walk($input, function ($entry, $source) use (&$output) {
foreach ($entry as $key=>$val) {
if(!array_key_exists($key, $output))
$output[$key] = [[$val, $source]];
else
$output[$key][] = [$val, $source];
}
});
echo print_r($output);
?>
PHP FIDDLE

Related

How to sum all same key values in multi-dimensional array where key have an array?

How can I add all the columnar values by associative key in PHP? Note that [1GB] key is dynamic
Array(
[0] => Array
(
[1GB] => array(1,2,1,3)
[2GB] => array(1,2,1,3)
)
[1] => Array
(
[1GB] => array(1,2,1,3)
[2GB] => array(1,2,1,3)
[5GB] => array(1,2,1,3)
)
[2] => Array
(
[3GB] => array(1,2,1,3)
[4GB] => array(1,2,1,3)
)
[2] => Array
(
[3GB] => array(1,2,1,3)
[4GB] => array(1,2,1,3)
)
)
Desired result :
Array(
[1GB] => array(2,3,2,6)
[2GB] => array(2,3,2,6)
[3GB] => array(1,2,1,3)
[4GB] => array(1,2,1,3)
[5GB] => array(1,2,1,3)
)
I'm probably misinterpreting something because I do not get the same output that you showed, in any case, it could help to achieve the answer you want
$dataset = [
[
['1GB' => [1, 2, 1, 3]],
['2GB' => [1, 2, 1, 3]],
],
[
['1GB' => [1, 2, 1, 3]],
['2GB' => [1, 2, 1, 3]],
['5GB' => [1, 2, 1, 3]],
],
[
['3GB' => [1, 2, 1, 3]],
['4GB' => [1, 2, 1, 3]],
],
[
['3GB' => [1, 2, 1, 3]],
['4GB' => [1, 2, 1, 3]],
]
];
$resume = [];
foreach ($dataset as $data) {
foreach ($data as $item) {
foreach ($item as $key => $value) {
if (!array_key_exists($key, $resume)) {
$resume[$key] = $value;
} else {
foreach ($value as $index => $number) {
$resume[$key][$index] += $number;
}
}
}
}
}
ksort($resume);
print_r($resume);
Output
Array
(
[1GB] => Array
(
[0] => 2
[1] => 4
[2] => 2
[3] => 6
)
[2GB] => Array
(
[0] => 2
[1] => 4
[2] => 2
[3] => 6
)
[3GB] => Array
(
[0] => 2
[1] => 4
[2] => 2
[3] => 6
)
[4GB] => Array
(
[0] => 2
[1] => 4
[2] => 2
[3] => 6
)
[5GB] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 3
)
)

PHP Merge arrays and add a value

I need help merging two PHP arrays:
Array 1:
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 6
)
Array 2:
Array
(
[0] => Array
(
[id_sabor] => 2
[chocolate] => N
)
[1] => Array
(
[id_sabor] => 3
[chocolate] => S
)
[2] => Array
(
[id_sabor] => 4
[chocolate] => N
)
[3] => Array
(
[id_sabor] => 5
[chocolate] => S
)
[4] => Array
(
[id_sabor] => 6
[chocolate] => N
)
)
The values on array 1 are the active objects. I need to keep on Array 2 or on a new array only the ones with an [id_sabor] that matches in the array 1 (in this case: 2, 3, 4 and 6). Also, on those that [chocolate]=S add a new value: [costo_extra]=25.
One way to do that could be to use array_reduce and use in_array to check if the first array contains the value of id_sabor.
$array1 = [2, 3, 4, 6];
$array2 = [
["id_sabor" => 1, "chocolate" => "N"],
["id_sabor" => 2, "chocolate" => "N"],
["id_sabor" => 3, "chocolate" => "S"],
["id_sabor" => 4, "chocolate" => "N"],
["id_sabor" => 5, "chocolate" => "S"],
["id_sabor" => 6, "chocolate" => "N"]
];
$array2 = array_reduce($array2, function($carry, $item) use ($array1){
if (in_array($item["id_sabor"], $array1)) {
if ($item["chocolate"] === "S") {
$item["costo_extra"] = 25;
}
$carry[] = $item;
}
return $carry;
});
Demo

Merging 3 arrays to one base on field

I have three array (is about the data migration)
$a = Array
(
[0] => Array
(
[0] => province
[1] => 701
[2] => AA
[3] => A
)
..
)
$b = Array
(
[0] => Array
(
[0] => district
[1] => 70101
[2] => BB
[3] => B
)
[1] => Array
(
[0] => district
[1] => 70102
[2] => BB1
[3] => B1
)
..
)
$c = Array
(
[0] => Array
(
[0] => commune
[1] => 7010101
[2] => CC
[3] => C
),
[1] => Array
(
[0] => commune
[1] => 7010102
[2] => CC1
[3] => C1
)
..
)
What I want is to merge all $a , $b , $c' to become a new array
in this example array that have value701is the key of sub array70101and70101is the key of sub array7010101`
So the final array may look something like this:
$d = array (
701=>array(
70101=>array(7010101,7010102),
70102=>array(7010201,7010202),
),
)
The attempt is like the following:
# Your data structure here:
$a = array(
'701' => 'foo',
'702' => 'bar',
);
$b = array(
'70101' => 'foo-foo',
'70102' => 'foo-bar',
);
$c = array(
'7010101' => 'foo-foo-foo',
'7010102' => 'foo-foo-bar',
'7020101' => 'bar-foo-foo',
'7020201' => 'bar-bar-foo',
);
# The array you want
$buffer = array();
# Loop through the deepest elements (here: commune)
foreach ($c as $key => $value) {
# Find the keys for the parent groups
$province_key = substr($key, 0, 3);
$district_key = substr($key, 0, 5);
# Fill the buffer
$buffer[$province_key][$district_key][$key] = $value;
}
# Debug: The generated array
echo '<pre>';
print_r($buffer);
echo '</pre>';
You can copy&paste it here and hit run.
Test data:
$arrayA = [
[
0 => 'province',
1 => 701,
2 => 'AA',
3 => 'A'
],
[
0 => 'province',
1 => 702,
2 => 'AA1',
3 => 'A1'
],
];
$arrayB = [
[
0 => 'district',
1 => 70102,
2 => 'BB',
3 => 'B'
],
[
0 => 'district',
1 => 70101,
2 => 'BB1',
3 => 'B1'
],
];
$arrayC = [
[
0 => 'commune',
1 => 7010101,
2 => 'CC',
3 => 'C'
],
[
0 => 'commune',
1 => 7010102,
2 => 'CC1',
3 => 'C1'
]
];
Solution:
function mergeArraysToOneOnField(array $arrayA, array $arrayB, array $arrayC, $fieldName) {
$result = [];
/*
checks like
!is_string($fieldName) && !is_integer($fieldName)
*/
$arrayARelevantFields = array_column($arrayA, $fieldName);
$arrayBRelevantFields = array_column($arrayB, $fieldName);
$arrayCRelevantFields = array_column($arrayC, $fieldName);
foreach ($arrayARelevantFields as $arrayARelevantField) {
$arrayAFilteredRelevantField = filterArrayByStrpos($arrayBRelevantFields, $arrayARelevantField);
foreach ($arrayAFilteredRelevantField as $arrayBRelevantField) {
$result[$arrayARelevantField][$arrayBRelevantField] =
filterArrayByStrpos($arrayCRelevantFields, $arrayBRelevantField)
;
}
}
return $result;
}
Test run:
$mergedArray = mergeArraysToOneOnField($arrayA, $arrayB, $arrayC, 1);
print_r($mergedArray);
Test Result:
Array
(
[701] => Array
(
[70102] => Array
(
)
[70101] => Array
(
[0] => 7010101
[1] => 7010102
)
)
)
The solution can be extended with recursion, in order to hanlde a variable number of input arrays:
function mergeArraysToOneOnField(array &resultArray, array $inputAarray, $field) {
...
}
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
Output
Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )

Multidimensional Array Find and Update The Value using PHP

I can't get suitable title for this thread (help me). I can't describe this problem so here the example of my problem.
My array :
Array ( [0] => Array ( [answer] => a [score] => 3 )
[1] => Array ([answer] => b [score] => 4 )
[2] => Array ( [answer] => h [score] => 3)
[3] => Array ( [answer] => a [score] => 4 ))
...
And I wanna get an output like this :
Array ( [0] => Array ( [answer] => a [score] => 7 )
[1] => Array ([answer] => b [score] => 4 )
[2] => Array ( [answer] => h [score] => 3))
...
You can see a change of score subkey in index key 0. This is happen because there is two value 'a' in answer subkey from index key 0 and 3. The score changed to 7 because of the sum of both (3+4). Really I don't have an idea for this, sorry for my english and thanks for help.
Feel free to comment. :)
$merged = array();
foreach ($array as $answer) {
if (isset($merged[$answer['answer']])) {
$merged[$answer['answer']]['score'] += $answer['score'];
} else {
$merged[$answer['answer']] = $answer;
}
}
var_dump($merged);
Check this answer, not using loop :
$arr = array ( array ( 'answer' => 'a', 'score' => 3 ),
array ( 'answer' => 'b', 'score' => 4 ),
array ( 'answer' => 'h', 'score' => 3),
array ( 'answer' => 'a', 'score' => 4 ));
$t = array_reduce($arr, function($result, $item) {
if(array_key_exists($item['answer'],$result)){
$result[$item['answer']] = array('answer' => $item['answer'], 'score' => $item['score']+$result[$item['answer']]['score']);
}
else{
$result[$item['answer']] = array('answer' => $item['answer'], 'score' => $item['score']);
}
return $result;
},array());
echo "<pre>";
print_r($t);
Output :
Array
(
[a] => Array
(
[answer] => a
[score] => 7
)
[b] => Array
(
[answer] => b
[score] => 4
)
[h] => Array
(
[answer] => h
[score] => 3
)
)
I though of using a temporary array:
/* Current array */
$array = array(
array("answer" => "a", "score" => 3),
array("answer" => "b", "score" => 4),
array("answer" => "h", "score" => 3),
array("answer" => "a", "score" => 4)
);
/* Using a temporary array */
$tmp_array = array();
foreach($array as $subarray){
if(array_key_exists($subarray["answer"], $tmp_array)){
$tmp_array[$subarray["answer"]] += $subarray["score"];
}else{
$tmp_array[$subarray["answer"]] = $subarray["score"];
}
}
/* Creating a new formatted array */
$new_array = array();
foreach($tmp_array as $key => $value){
$new_array[] = array("answer" => $key, "score" => $value);
}
print_r($new_array);

Merge two multidimensional arrays and reindex all subarrays

I have two arrays, I want to merge these two arrays into single array. Please view the detail below:
First Array:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
[1] => Array
(
[a] => 3
[b] => 2
[c] => 1
)
)
Second Array:
Array
(
[0] => Array
(
[d] => 4
[e] => 5
[f] => 6
)
[1] => Array
(
[d] => 6
[e] => 5
[f] => 4
)
)
I want this result. Does somebody know how to do this?
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
[2] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[3] => Array
(
[0] => 6
[1] => 5
[2] => 4
)
)
Hope you have understand the question.
Thank you in advance.
Try array_merge:
$result = array_merge($array1, $array2);
FIXED (again)
function array_merge_to_indexed () {
$result = array();
foreach (func_get_args() as $arg) {
foreach ($arg as $innerArr) {
$result[] = array_values($innerArr);
}
}
return $result;
}
Accepts an unlimited number of input arrays, merges all sub arrays into one container as indexed arrays, and returns the result.
EDIT 03/2014: Improved readability and efficiency
more simple and modern way is:
$merged = $array1 + ['apple' => 10, 'orange' => 20] + ['cherry' => 12, 'grape' => 32];
new array syntax from php 5.4
If you want to return the exact result you specify in your question then something like this will work
function array_merge_no_keys() {
$result = array();
$arrays = func_get_args();
foreach( $arrays as $array ) {
if( is_array( $array ) ) {
foreach( $array as $subArray ) {
$result[] = array_values( $subArray );
}
}
}
return $result;
}
As a purely native function solution, merge the arrays, then reindex each subarray.
Code: (Demo)
$a = [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 3, 'b' => 2, 'c' => 1],
];
$b = [
['d' => 4, 'e' => 5, 'f' => 6],
['d' => 6, 'e' => 5, 'f' => 4],
];
var_export(
array_map('array_values' array_merge($a, $b))
);
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 3,
1 => 2,
2 => 1,
),
2 =>
array (
0 => 4,
1 => 5,
2 => 6,
),
3 =>
array (
0 => 6,
1 => 5,
2 => 4,
),
)

Categories