Merge all subarrays on second level of a multidimensional array [duplicate] - php

This question already has answers here:
How to array_merge_recursive() an array?
(2 answers)
Closed 5 months ago.
I need to find a way to combine elements by key in a new array, such that I end up with just one key containing all values for that key (found elsewhere in the parent array). This will make more sense when you see the example below:
Array
(
[0] => Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
)
[1] => Array
(
[AAA] => Array
(
[0] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)
)
The original array is quite large with more keys and values. But everything is in the same structure depicted above.
I'd like to read that in, and return
Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
[4] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)
What's the least painful and most efficient way of accomplishing this?

Using array_merge_recursive() and array unpacking to pass the separate parts into it...
print_r(array_merge_recursive(...$array));
gives...
Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
[3] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)

You could use array_map() to traverse the parent array. And use another foreach loop to combine the values. Please note the $output array is passed by reference.
$inputArray = [
0 => [
'AAA' => [
0 => 'F104_f58f85f9dbbe1ce083ca09a6f9ace679',
1 => 'F104_480e9f231b2b5a138f8aa5570ffa8634',
2 => 'F104_d5fde0ad2499052e0eae6dec451f9385',
],
'BBB' => [
0 => 'F44_e2e052e7b78abbae02ffcf7413302a0c',
1 => 'F44_4d4da736509babebba7433b203b16753',
]
],
1 => [
'AAA' => [
0 => 'F104_480e9f231b2b5a138f8aa5570ffa8634'
],
'ZZZ' => [
0 => 'F20_5e742bc21f41a14243c3f72b9fa4815e'
]
]
];
$output = [];
array_map(function($values) use (&$output){
foreach ($values as $key => $value){
if(empty($output[$key])){
$output[$key] = $value;
}else{
$output[$key] = array_merge($output[$key], $value);
}
}
}, $inputArray);
print_r($output);
Here is a working example http://sandbox.onlinephpfunctions.com/code/97909ce93184fad6c33af8eb48d184ae86438774

Related

Append one array values as key value pair to another array php

I have two arrays both guaranteed to be the same length. The two arrays are of the following structures
array1
Array
(
[0] => Array
(
[id] => 841052
[store] => 11
[position] => 1
)
[1] => Array
(
[id] => 1613197
[store] => 11
[position] => 401
)
[2] => Array
(
[id] => 1648966
[store] => 11
[position] => 1
)
[3] => Array
(
[id] => 1656857
[store] => 11
[position] => 1
)
....
....
)
array2
Array
(
[0] => 5/20/2019
[1] => 7/7/2019
[2] => 12/16/2018
...
...
)
How do I append every value of array2 as a key value pair to array1 to get the following array. The key name can be whatever I just chose date.
Array
(
[0] => Array
(
[id] => 841052
[store] => 11
[position] => 1
[date] => 5/20/2019
)
[1] => Array
(
[id] => 1613197
[store] => 11
[position] => 401
[date] => 7/7/2019
)
[2] => Array
(
[id] => 1648966
[store] => 11
[position] => 1
[date] => 12/16/2018
)
)
...
...
...
I have tried
array_push($array1, $array2);
It just pushed it to the last element of the array. I thought of using two foreach loops but couldn't get ti to work. Is there a built in php function that will do this, or do I have to do it in loops.
Just walk $array1 and modify each sub-array by adding the new key and the value of $array2 with the same key:
array_walk($array1, function(&$v, $k) use($array2) { $v['date'] = $array2[$k]; });
try this:
$array1 = array(array("id" => 841052, "store" => "11", "position" => "1"), array("id" => 1613197, "store" => "11", "position" => "401"),);
$array2 = array("5/20/2019", "7/7/2019");
foreach ($array1 as $index => $valuearray1) {
if (array_key_exists($index, $array2)) {
$array1[$index]["date"] = $array2[$index];
}
}
var_dump($array1);

Sorting multidimensional array in PHP with key values [duplicate]

This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 5 years ago.
I need to sort this array below, in descending order, but I'm currently stuck and can't think of a simple solution.
This is the array structure:
$data = Array
(
[Name1] => Array
(
[1] => 40-45
[0] => 124791.63
)
[Name2] => Array
(
[1] => 46
[0] => 2624.7
)
[Name3] => Array
(
[1] => 50
[0] => 37784.27
)
[Name4] => Array
(
[1] => 52
[0] => 1008
)
)
I want to sort it by value $data[Name1][0], in the descending order, as you can see the current array is not sorted since the values go like this:
124791.63 -- 2624.7 -- 37784.27 ...etc
Is there any simple solution to this? I googled, but I couldn't find an answer to this specific problem.
An option to do this is use uasort.
For example:
$data = [
'Name1' => [
"124791.63",
"40-45"
],
'Name2' => [
"2624.7",
"46"
],
'Name3' => [
"37784.27",
"50"
],
'Name4' => [
"1008",
"52"
]
];
function sortByValue($a, $b) {
return $a[0] < $b[0];
}
uasort($data, 'sortByValue');
echo "<pre>";
print_r($data);
Will result in:
Array
(
[Name1] => Array
(
[0] => 124791.63
[1] => 40-45
)
[Name3] => Array
(
[0] => 37784.27
[1] => 50
)
[Name2] => Array
(
[0] => 2624.7
[1] => 46
)
[Name4] => Array
(
[0] => 1008
[1] => 52
)
)

how to find difference in single dimensional array with single dimensional array in php

I have two array two array. First is multidimensional and other is single dimensional. I want to find difference between them. How do I found.
$arrayresult
Array 1
Array (
[0] => Array ( [0] => ishani.lad [1] => 9033187384 )
[1] => Array ( [0] => rajkumar.prajapati [1] => 8460078459 )
[2] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[3] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[4] => Array ( [0] => vishal.dake [1] => 9879815299 )
[5] => Array ( [0] => mohsin [1] => 8347163123 )
)
$useduser
Array 2
Array (
[0] => ishani.lad
[1] => rajkumar.prajapati
[2] => lokesh.bhandari
)
I need difference as result as below
Result
Array (
[0] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[1] => Array ( [0] => vishal.dake [1] => 9879815299 )
[2] => Array ( [0] => mohsin [1] => 8347163123 )
)
I have used solution as
$resultremainig = [];
foreach($arrayresult as $val2){
if(!in_array($val2[0], $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
}
But it show last record also. Result of above code is as below. It always show me last record in second array's also
Array (
[0] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[1] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[2] => Array ( [0] => vishal.dake [1] => 9879815299 )
[3] => Array ( [0] => mohsin [1] => 8347163123 )
)
If you wanted you could try using nested loops like so:
<?php
$arrOne = $arrFinal = [
["ishani.lad", 9033187384],
["rajkumar.prajapati", 8460078459],
["lokesh.bhandari" , 9687060900],
["shishanshu.rai" , 8401915337],
["vishal.dake" , 9879815299],
["mohsin" , 8347163123],
];
$arrTwo = [
"ishani.lad",
"rajkumar.prajapati",
"lokesh.bhandari",
];
foreach($arrOne as $key=>$item){
foreach($arrTwo as $k=>$v){
if(in_array($v, $item)){
unset($arrFinal[$key]);
}
}
}
var_dump($arrFinal);
// PRODUCES:::
array (size=3)
3 =>
array (size=2)
0 => string 'shishanshu.rai' (length=14)
1 => int 8401915337
4 =>
array (size=2)
0 => string 'vishal.dake' (length=11)
1 => int 9879815299
5 =>
array (size=2)
0 => string 'mohsin' (length=6)
1 => int 8347163123
You could use array_filter():
$output = array_filter($arrayresult, function($a) use ($useduser) {
return !in_array($a[0], $useduser);
});
Hi You can also try this
$one = array(array('ishani.lad',9033187384),array('rajkumar.prajapati',8460078459),array('lokesh.bhandari',9687060900),array('shishanshu.rai',8401915337),array('vishal.dake',9879815299),array('mohsin',8347163123));
$two = array('ishani.lad','rajkumar.prajapati','lokesh.bhandari');
foreach($one as $array){
if(!in_array($array[0],$two)){
$final[] = $array;
}
}
echo "<pre>";print_r($final);
Output
Array
(
[0] => Array
(
[0] => shishanshu.rai
[1] => 8401915337
)
[1] => Array
(
[0] => vishal.dake
[1] => 9879815299
)
[2] => Array
(
[0] => mohsin
[1] => 8347163123
)
)
Trim the value before checking in $useduser array
$resultremainig = [];
foreach($arrayresult as $val2){
// this removes any extra spaces from the search string
if(!in_array(trim($val2[0]), $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
You need to use the array_diff function.
Store your 2 arrays in variables and compare them.

merging of 2 identical keys in one array

Need help from the team,
I have this scenario of having 2 identical keys in each array with different values, i want them to be merged into one key were the values also are in it
example:
arrayData1(
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
)
)
arrayData2(
[2] => Array
(
[EXP3] => Array (records...)
[EXP4] => Array (records...)
)
)
Having the output like this:
arrayFinal (
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
[EXP3] => Array (records...)
[EXP3] => Array (records...)
)
)
Thanks!
First of all you cannot have two same keys in a single array, what you can do is use the array_merge_recursive function in php to merge both the arrays, and the repeating keys will have a new array with all the repeating key values..
$array1 = [
'EXP1' => [1,2,3],
'EXP2' => [2,3,4]
];
$array2 = [
'EXP2' => [5,6,7],
'EXP3' => [8,9,10]
];
Now there are two EXP2 keys, so when you use array_merge_recursive() you get something like this,
print_r(array_merge_recursive($array1, $array2));
//output Array (
[EXP1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[EXP2] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
)
[EXP3] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)

Sort multidimensional array by multidimensional array value

I want to use the 'id' array of Array 1 to order all other sibling arrays in Array 2. The 'id' array is supposed to act as the schema for all other arrays in $array1 and $array2.
This is easier explained with an example.
I have the following array, Array 1 :
Array
(
[id] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[headline] => Array
(
[0] => test 1
[1] => test 3
[2] => test 2
)
)
And another array, Array 2:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[headline] => Array
(
[0] => tc test 1
[1] => tc test 2
[2] => tc test 3
)
)
That I would like to look like the following:
Array
(
[id] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[headline] => Array
(
[0] => tc test 1
[1] => tc test 3
[2] => tc test 2
)
)
Right now I'm trying to force it with foreach statements (and failing), but there has to be a better way.
This is a “fake” sort, because I fill an empty array through a foreach() loop, then I replace original array:
$array1 = [ 'id' => [ 1,3,2 ], 'headline' => [ 'test 1','test 3','test 2' ] ];
$array2 = [ 'id' => [ 1,2,3 ], 'headline' => [ 'test 1','test 2','test 3' ] ];
$result = array();
foreach( $array1['id'] as $val )
{
$key = array_search( $val, $array2['id'] );
$result['id'][] = $array2['id'][$key];
$result['headline'][] = $array2['headline'][$key];
}
$array2 = $result;
print_r( $array2 );
Output:
Array
(
[id] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[headline] => Array
(
[0] => test 1
[1] => test 3
[2] => test 2
)
)
I execute a foreach loop through criterion 'id' array, find each value in array to be sorted, retrieve its key and use it to add values to resulting array.
Not so proud of this solution, but this is.

Categories