Merging 3 arrays to one base on field - php

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 )

Related

sum of unique values in array

I have below array $billitems_taxes
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 12
)
[1] => Array
(
[id] => 2
[tax_name] => A
[tax_value] => 8
)
[2] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
and I want output as below, find two common tax_name and do some of same and then create a new array.
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 20
)
[1] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
I tried with below code, but it did not return a correct array.
$return_array = [];
foreach($billitems_taxes as $b)
{
$return_array['tax_name'] = $b->tax_name;
$return_array['tax_value'] += $b->tax_value;
}
First off, you have an array of arrays, not objects.
Then your loop needs to know if it has already seen a this tax name which will already be in the new array to check that I used array_key_exists()
$return_array = [];
foreach($billitems_taxes as $b)
{
if ( array_key_exists($b['tax_name'], $return_array) ) {
$return_array[$b['tax_name']]['tax_value'] += $b['tax_value'];
} else {
$return_array[$b['tax_name']] = $b;
}
}
RESULT
Array(
[A] => Array
([id] => 1
[tax_name] => A
[tax_value] => 20
)
[B] => Array
([id] => 3
[tax_name] => B
[tax_value] => 12
)
)
And if its important for the array to be numerically indexed just add
$return_array = array_values($return_array);
after the end of the loop
You must group by 'tax_name' and must sum 'tax_value'.
$billitems_taxes = [
['id' => 1, 'tax_name' => 'A', 'tax_value' => 12],
['id' => 2, 'tax_name' => 'A', 'tax_value' => 8],
['id' => 3, 'tax_name' => 'B', 'tax_value' => 12]
];
$result = [];
foreach($billitems_taxes as $row){
$groupKey = $row['tax_name'];
if(array_key_exists($groupKey,$result)){
$result[$groupKey]['tax_value'] += $row['tax_value'];
} else {
$result[$groupKey] = $row;
}
}
$result = array_values($result);
echo '<pre>';
var_export($result);
/*
array (
0 =>
array (
'id' => 1,
'tax_name' => 'A',
'tax_value' => 20,
),
1 =>
array (
'id' => 3,
'tax_name' => 'B',
'tax_value' => 12,
),
)
*/
The solution with the external class tableArray is very simple. The result is the same.
$result = tableArray::create($billitems_taxes)
->filterGroupAggregate(['tax_value' => 'SUM'],['tax_name'])
->fetchAll()
;

Compare two array-values in multidimensional array [duplicate]

This question already has answers here:
Put multiple arrays in one large associative array
(2 answers)
Closed 2 years ago.
I am having two arrays with same keys from two different queries.
First query result:
Array
(
[0] => Array
(
[Contribution] => 1000.00
[P_Name] => A
)
[1] => Array
(
[Contribution] => 1500.00
[P_Name] => B
)
)
Second query result:
Array
(
[0] => Array
(
[Contribution] => 100.00
[P_Name] => A
)
[1] => Array
(
[Contribution] => 200.00
[P_Name] => B
)
)
The first array may be empty and/or the second may be empty.
I want to get the create a new array that finds the sum of Contribution values where P_Name values match, like this:
Array
(
[0] => Array
(
[Contribution] => 1100.00
[P_Name] => A
)
[1] => Array
(
[Contribution] => 1700.00
[P_Name] => B
)
)
I have tried array_merge():
$result1= $this->model1->getOthersTotal($date);
$result2=$this->model1->getMiscTotal($date);
$merged_result = array_merge( $result1, $result2 );
$merged_result contains:
Array (
[0] => Array (
[Contribution] => 1000.00
[P_Name] => A
)
[1] => Array (
[Contribution] => 1001.00
[P_Name] => A
)
[2] => Array (
[Contribution] => 69.00
[P_Name] => B
)
)
Input:
$a=[['Contribution'=>1000,'P_Name'=>'A'],
['Contribution'=>1500,'P_Name'=>'B'],
['Contribution'=>2000,'P_Name'=>'C']];
$b=[['Contribution'=>100,'P_Name'=>'A'],
['Contribution'=>200,'P_Name'=>'B'],
['Contribution'=>300,'P_Name'=>'D']];
If you temporarily assign associative keys to the subarrays using array_column(), then you can leverage array_merge_recursive() to group on P_Name values, then call array_sum() to do the addition if there is more than one value to for a given P_Name.
Method #1: (Demo)
$keyed=array_merge_recursive(array_column($a,NULL,'P_Name'),array_column($b,NULL,'P_Name'));
foreach($keyed as $p_name=>$array){
$result[]=['Contribution'=>(is_array($array['Contribution'])?array_sum($array['Contribution']):$array['Contribution']),'P_Name'=>$p_name];
}
var_export($result);
Or just do a standard merge to create one array, then loop and add as you go. Finalize the output array with array_values() to reindex the elements.
Method #2: (Demo)
foreach(array_merge($a,$b) as $array){
if(isset($result[$array['P_Name']])){
$result[$array['P_Name']]['Contribution']+=$array['Contribution'];
}else{
$result[$array['P_Name']]=$array;
}
}
$result=array_values($result);
var_export($result);
Output: (from either method)
array (
0 =>
array (
'Contribution' => 1100,
'P_Name' => 'A',
),
1 =>
array (
'Contribution' => 1700,
'P_Name' => 'B',
),
2 =>
array (
'Contribution' => 2000,
'P_Name' => 'C',
),
3 =>
array (
'Contribution' => 300,
'P_Name' => 'D',
),
)
It is out of the scope of this question, but chances are the best approach would be to perform this grouping/addition via database query.
Here is a quick and dirty way to do it: Loop over both arrays, the outer loop fills the inner loop. If no match was fount, $x remails 0, and the value will be added to the inner loop. If a match is found, $x is 1 and the inner loop will break to continue the outer loop.
$a = [['a' => 10,'b' => 'g'], ['a' => 11,'b' => 'h']];
$b = [['a' => 1, 'b' => 'g'], ['a' => 2, 'b' => 'h'], ['a' => 3, 'b' => 'i']];
// now its fool proof.
function mergeData( $a, $b )
{
if( empty( $a ) && empty( $b ) )
return [];
if( empty( $a ) )
return $b;
if( empty( $b ) )
return $a;
foreach( $b AS $i => $c ) {
$x = 0;
foreach( $a AS $ii => $d ) {
if( $c['b'] == $d['b'] ) {
$a[ $ii ]['a'] += $c['a'];
$x = 1;
break;
}
}
if( !$x )
$a[] = $b[ $i ];
}
return $a;
}
Output
Array
(
[0] => Array
(
[a] => 11
[b] => g
)
[1] => Array
(
[a] => 13
[b] => h
)
[2] => Array
(
[a] => 3
[b] => i
)
)
Little bit different approach
$array1 = [
[
'Contribution' => 10,
'P_Name' => 'A'
],
[
'Contribution' => 1500,
'P_Name' => 'B'
]
];
$array2 = [
[
'Contribution' => 200,
'P_Name' => 'B'
],
[
'Contribution' => 100,
'P_Name' => 'C'
],
];
$array3 = array_map(function($elem) use (&$array2){
foreach($array2 as $i => &$a2){
if($a2['P_Name'] == $elem['P_Name']){
$a2['Contribution'] += $elem['Contribution'];
return;
}
}
return $elem;
},$array1);
$array3 = array_merge(array_filter($array3),$array2);
print_r($array3);
output:
Array
(
[0] => Array
(
[Contribution] => 10
[P_Name] => A
)
[1] => Array
(
[Contribution] => 1700
[P_Name] => B
)
[2] => Array
(
[Contribution] => 100
[P_Name] => C
)
)
You can use array_reduce(), array_map(), and array_sum():
<?php
function merge(array ...$sets)
{
/**
* group contributions by name
*/
$contributions = array_reduce(
$sets,
function (array $contributions, array $set) {
foreach ($set as $element) {
$name = $element['P_Name'];
$contribution = $element['Contribution'];
if (!array_key_exists($name, $contributions)) {
$contributions[$name] = [];
}
$contributions[$name][] = $contribution;
}
return $contributions;
},
[]
);
/**
* normalize the array so we remove the name as key, and return a tuple of name and contribution, with the desired
* structure
*/
return array_values(array_map(function (array $contribution, $name) {
return [
'Contribution' => array_sum($contribution),
'P_Name' => $name,
];
}, $contributions, array_keys($contributions)));
}
$a = [
[
'Contribution' => 1000,
'P_Name' => 'A',
],
[
'Contribution' => 1500,
'P_Name' => 'B',
],
];
$b = [
[
'Contribution' => 100,
'P_Name' => 'A',
],
[
'Contribution' => 200,
'P_Name' => 'B',
],
];
$merged = merge($a, $b);
var_dump($merged);
Note Because of using variadics, any number of arrays can be passed to merge(). Requires at least PHP 5.6, though.
For reference, see:
http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list
http://php.net/manual/en/function.array-reduce.php
http://php.net/manual/en/function.array-key-exists.php
http://php.net/manual/en/function.array-map.php
http://php.net/manual/en/function.array-sum.php
http://php.net/manual/en/function.array-values.php
http://php.net/manual/en/function.array-keys.php
For an example, see:
https://3v4l.org/EI4Tn

merge 2 array with different number of keys

I have 2 arrays
$arr1 = Array
(
[REG1] => 94
[REG3] => 45
)
$arr2 =Array
(
[0] => REG1
[1] => REG2
[2] => REG3
[3] => REG4
)
I have to loop 2 arrays and I would like a result in one array like this:
Array(
[0] => Array(
[REG1] => 94
)
[1] => Array(
[REG2] =>
)
[2] => Array(
[REG3] => 45
)
[3] => Array(
[REG4] =>
)
)
But for the instand I can not do what I want, here is where I am:
private function mergeData($arr1, $arr2){
$result = array_map(function($v1, $v2){
$t[$v1] = $v2;
return $t;
}, $arr2, $arr1);
return $result;
}
output:
Array(
[0] => Array(
[REG1] => 94
)
[1] => Array(
[REG2] =>45
)
[2] => Array(
[REG3] =>
)
[3] => Array(
[REG4] =>
)
)
I cannot correctly put the bone values with the right keys.
I tried with array_merge_recursive but also failed.
Thanks for your help
$arr3 = array_fill_keys( $arr2, '' );
creates an array containing the values of $arr2 as keys with as value an empty string.
$arr3 =Array
(
[REG1] => ''
[REG2] => ''
[REG3] => ''
[REG4] => ''
)
After that just merge.
$result = array_merge ( $arr3, $arr1 );
Another option would be to use the array_map function and map key with the corresponding values, if any:
$arr1 = [
'REG1' => 94,
'REG3' => 45
];
$arr2 = [
'REG1',
'REG2',
'REG3',
'REG4'
];
$result = array_map(function($item) use($arr1){
return [$item => isset($arr1[$item]) ? $arr1[$item] : ""];
},$arr2);
This will return:
Array
(
[0] => Array
(
[REG1] => 94
)
[1] => Array
(
[REG2] =>
)
[2] => Array
(
[REG3] => 45
)
[3] => Array
(
[REG4] =>
)
)
Using a loop:
<?php
$arr1 = [
'REG1' => 94,
'REG3' => 45
];
$arr2 = [
'REG1',
'REG2',
'REG3',
'REG4'
];
$result = $arr2;
foreach($result as &$v)
$v = [$v => $arr1[$v] ?? null];
unset($v);
var_export($result);
Output:
array (
0 =>
array (
'REG1' => 94,
),
1 =>
array (
'REG2' => NULL,
),
2 =>
array (
'REG3' => 45,
),
3 =>
array (
'REG4' => NULL,
),
)

PHP make some of the Key to be sticky in an Associative array

I try to use array_column and array_multisort to sort Array B by Key (ID).
However, I have a scenario whereby I need to have some ID to be sticky on the top .
For example, by comparing Array A and Array B, move ID 3 and ID 1 to the top of the Array B. The final result will be Array C .
Is there a PHP array function to achieve this? Please advice...
Array A
Array
(
[0] => Array
(
[ID] => 3
)
[1] => Array
(
[ID] => 1
)
)
1
Array B
Array
(
[0] => Array
(
[ID] => 1
[product] => A
)
[1] => Array
(
[ID] => 2
[product] => B
)
[2] => Array
(
[ID] => 3
[product] => C
)
[3] => Array
(
[ID] => 4
[product] => D
)
)
1
Array C (Result)
Array
(
[0] => Array
(
[ID] => 3
[product] => C
)
[1] => Array
(
[ID] => 1
[product] => A
)
[2] => Array
(
[ID] => 2
[product] => B
)
[3] => Array
(
[ID] => 4
[product] => D
)
)
1
The preparatory steps involved in generating the lookup and outlier values may be avoidable with more intimate knowledge of your project scope, but I'll only use the values that you have provided. (I mean, you could hardcode outlier as 9999999 and wrap array_flip around array_column for brevity.)
The max value simply needs to be a value higher than the highest ID in the priority array.
The lookup needs to have ID values as keys and their original indexes as the new values (hence the flip).
usort() is precisely the php function to use when performing a custom sorting process.
use is necessary to pass the required additional values into the custom function's scope.
The spaceship operator (<=>) is a fantastic way to package your two-condition sort logic. Each side will be compared using the values "left to right".
Code: (Demo)
$priority = [['ID' => 3], ['ID' => 1]];
$input = [['ID' => 1, 'product' => 'A'], ['ID' => 2, 'product' => 'B'], ['ID' => 3, 'product' => 'C'], ['ID' => 4, 'product' => 'D']];
$lookup = array_column($priority, 'ID');
$outlier = max($lookup) + 1;
$lookup = array_flip($lookup);
usort($input, function($a, $b) use ($lookup, $outlier) {
return [$lookup[$a['ID']] ?? $outlier, $a['ID']] <=> [$lookup[$b['ID']] ?? $outlier, $b['ID']];
});
var_export($input);
Output:
array (
0 =>
array (
'ID' => 3,
'product' => 'C',
),
1 =>
array (
'ID' => 1,
'product' => 'A',
),
2 =>
array (
'ID' => 2,
'product' => 'B',
),
3 =>
array (
'ID' => 4,
'product' => 'D',
),
)
Without the spaceship operator, the custom function gets noticeably more verbose.
Code: (Demo)
usort($input, function($a, $b) use ($lookup, $outlier) {
$bIsPriority = isset($lookup[$b['ID']]);
if (isset($lookup[$a['ID']])) {
if ($bIsPriority) {
return $lookup[$a['ID']] - $lookup[$b['ID']];
} else {
return -1;
}
} elseif ($bIsPriority) {
return 1;
} else {
return $a['ID'] - $b['ID'];
}
});
Here is one of doing it:
<?php
$a = [['ID' => 5], ['ID' => 1]];
$b = [['ID' => 1, 'product' => 'A'], ['ID' => 2, 'product' => 'B'], ['ID' => 3, 'product' => 'C'], ['ID' => 4, 'product' => 'D'], ['ID' => 5, 'product' => 'E']];
// $keysOnTop = Array([0] => 5, [1] => 1)
$keysOnTop = array_column($a, 'ID');
$temp1 = $temp2 = [];
foreach($b as $value){
if(in_array($value['ID'], $keysOnTop)){
$temp1[] = $value;
} else {
$temp2[] = $value;
}
}
// $temp1 = Array([0] => Array([ID] => 1, [product] => A), [1] => Array([ID] => 5, [product] => E))
// $temp2 = Array([0] => Array([ID] => 2, [product] => B), [1] => Array([ID] => 3, [product] => C), [2] => Array([ID] => 4, [product] => D))
$final_arr = array_merge($temp1, $temp2);
echo '<pre>';
print_r($final_arr);
// Output:
/*
Array
(
[0] => Array
(
[ID] => 1
[product] => A
)
[1] => Array
(
[ID] => 5
[product] => E
)
[2] => Array
(
[ID] => 2
[product] => B
)
[3] => Array
(
[ID] => 3
[product] => C
)
[4] => Array
(
[ID] => 4
[product] => D
)
)
*/
?>

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);

Categories