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
)
)
Related
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
multidimensional array
Array
(
[0] => Array
(
[ID] => 5068
[Item] => 2737
[Unit] => 15
)
[1] => Array
(
[ID] => 5067
[Item] => 2737
[Unit] => 13
)
)
single dimensional array
Array (
[0] => 11
[1] => 13
[2] => 15
)
Expected output:
Array
(
[0] => Array
(
[ID] => 5067
[Item] => 2737
[Unit] => 13
)
[1] => Array
(
[ID] => 5068
[Item] => 2737
[Unit] => 15
)
)
It not necessary that single dimensional array must be in ASC/DESC order. I want to reorder multidimensional array according to single dimensional array based on unit value. How can I achieve this?
Maybe something like this would work?
$multiDimArray = [
0 => [
'ID' => 5068,
'Item' => 2737,
'Unit' => 15
],
1 => [
'ID' => 5067,
'Item' => 2737,
'Unit' => 13
]
];
$singleDim = [
11,13,15
];
usort($multiDimArray,function($a,$b){
global $singleDim;
return ($singleDim[0] >= $a['Unit']) ? -1 :1;
});
print_r($multiDimArray);
there's something in back of my head that tells me I'm missing something. But feel free to give it a go.
Assuming that the values in the single array all map to a value in Unit, you could loop the single dimensional array and then loop the values in the multidimensional array.
In the inner loop use in_array to check if the value from the single dimensional array occurs in the multidimensional array. If it does, you could for example add it to a new array.
$result = [];
foreach ($singleDimension as $sm) {
foreach ($multiDimensional as $md) {
if (in_array($sm, $md)) {
$result[] = $md;
}
}
}
Demo
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
)
)
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.
Found a question which already explains this - How to sort an array of associative arrays by value of a given key in PHP?
I have an array (A1) of arrays (A2). I would like to sort all A2s inside A1 based on a key:value pair in A2
The structure is like this
A1
->A2-1
->K1:Some Value
->K2:ValB
->A2-2
->K1:Other Value
->K2:ValB1
->A2-3
->K1:Some Value
->K2:ValB2
->A2-4
->K1:Other Value
->K2:ValB3
I would like to sort the arrays in A1 so that all A2s for which K1's value is Other Value are bunched together and all A2s for which K1's value is Some Value are bunched together
So after the sorting, the final order of arrays in A1 should be A2-2, A2-4, A2-1, A2-3. Is there a function in PHP using which I can do this? Or do I have to parse through the entire array and do the sorting?
Thanks.
http://php.net/manual/en/function.array-multisort.php
If that doesn't fit, there's a lot of other array functions http://www.php.net/manual/en/ref.array.php
Have a try with:
$A1 = array(
'A2-1' => array(
'K1' => 'Some Value',
'K2' => 'ValB',
),
'A2-2' => array(
'K1' => 'Other Value',
'K2' => 'ValB1',
),
'A2-3' => array(
'K1' => 'Some Value',
'K2' => 'ValB2',
),
'A2-4' => array(
'K1' => 'Other Value',
'K2' => 'ValB3',
)
);
function mySort($a, $b) {
if ($a['K1'] == $b['K1']) {
return strcmp($a['K2'], $b['K2']);
} else {
return strcmp($a['K1'], $b['K1']);
}
}
uasort($A1, 'mySort');
print_r($A1);
output:
Array
(
[A2-2] => Array
(
[K1] => Other Value
[K2] => ValB1
)
[A2-4] => Array
(
[K1] => Other Value
[K2] => ValB3
)
[A2-1] => Array
(
[K1] => Some Value
[K2] => ValB
)
[A2-3] => Array
(
[K1] => Some Value
[K2] => ValB2
)
)
You need something like:
usort($array, function($a, $b)
{
return strcmp($a['k1'], $b['k1']);
});
You may need to replace strcmp with a different sorting function (or operators) because it's unclear exactly what you are doing.
strategy:
1. find unique values, put them aside.
2. loop through unique values
2.1 loop origial array
2.2 store sub array in $out if unique val = original val
<?php
$i=0;
$a3d = array(
$i++ => array(0=>'Some Value',1=>'ValB'),
$i++ => array(0=>'Other Value',1=>'ValB1'),
$i++ => array(0=>'Some Value',1=>'ValB2'),
$i++ => array(0=>'Zome moar',1=>'ValB4'),
$i++ => array(0=>'Other Value',1=>'ValB3'),
$i++ => array(0=>'Zome Moar',1=>'ValB4'),
);
print_r($a3d);
foreach ($a3d as $a2d){
$uniq[]= $a2d[0];
}
$uniq = array_unique($uniq);
sort($uniq);
print_r($uniq);
foreach ($uniq as $v){
foreach ($a3d as $kk => $a2d){
if ($a2d[0] == $v){
$out[]= $a2d;
unset($a3d[$kk]); // <--avoid rechecking elements
}
}
}
print_r(count($a3d));
print_r($out);
?>
$ php sort_array.php
Array
(
[0] => Array
(
[0] => Some Value
[1] => ValB
)
[1] => Array
(
[0] => Other Value
[1] => ValB1
)
[2] => Array
(
[0] => Some Value
[1] => ValB2
)
[3] => Array
(
[0] => Zome moar
[1] => ValB4
)
[4] => Array
(
[0] => Other Value
[1] => ValB3
)
[5] => Array
(
[0] => Zome Moar
[1] => ValB4
)
)
Array
(
[0] => Other Value
[1] => Some Value
[2] => Zome Moar
[3] => Zome moar
)
0Array
(
[0] => Array
(
[0] => Other Value
[1] => ValB1
)
[1] => Array
(
[0] => Other Value
[1] => ValB3
)
[2] => Array
(
[0] => Some Value
[1] => ValB
)
[3] => Array
(
[0] => Some Value
[1] => ValB2
)
[4] => Array
(
[0] => Zome Moar
[1] => ValB4
)
[5] => Array
(
[0] => Zome moar
[1] => ValB4
)
)