php sort array by sub-value - php

here is my array, I think i should be using usort but i have no idea how to get that working.
Array
(
[javascript] => Array
(
[core] => Array
(
[0] => Array
(
[file] => /path/to/file.js
[weight] => 0
)
[1] => Array
(
[file] => /path/to/file2.js
[weight] => 1
)
)
)
)
I would like to sort the index of core by the weight value, other files and weights will be added and sorted after also.

usort($array['javascript']['core'], function($a, $b) {
return $a['weight'] - $b['weight'];
});

Related

PHP - How to sort multidimensional array by Key?

I have to sort multidimensional array by key and I need some recomendation how to do it.
I tried using ksort() and other php built-in functions, but none of them helped me.
This is my array, which I want to sort by key:
Array
(
[0] => Array
(
[20190529] => Array
(
[30] => Array
(
[17] => Array
(
[3846] => 0
)
)
)
)
[1] => Array
(
[20190516] => Array
(
[31] => Array
(
[17] => Array
(
[512] => 0
)
)
)
)
)
In that case, keys are 20190529 and 20190516
Working demo.
You can use array_multisort to achieve your requirement.
$keys = [];
foreach($arr as $k => $item){
$keys[] = key($item);
}
array_multisort($keys, SORT_NATURAL, $arr);
array_multisort — Sort multiple or multi-dimensional arrays
SORT_NATURAL - compare items as strings using "natural ordering" like natsort().
I modified my array like that:
Array
(
[0] => Array
(
[DATE] => 20190503
[DEAL] => 30
[IBLOCK] => 18
[AMOUNT] => 2500
[PAYED] => 0
)
[1] => Array
(
[DATE] => 20190516
[DEAL] => 31
[IBLOCK] => 17
[AMOUNT] => 512
[PAYED] => 0
)
)
then I used usort():
function cmp($a, $b) {
return $a['DATE'] - $b['DATE'];
}
usort($mydata, "cmp");
And it did job for me :) but Now I have to modify my array again to return first look.

Merge array into multidimensional array

How can I merge two arrays in the way I explain below?
Using print_r(), this is the output of the two arrays;
The first one;
Array
(
[created] => 1
[approved] => 1
)
And the second array;
Array
(
[created] => Array
(
[label] => Order created
[roles] => Array
(
[0] => ROLE_USER
)
[next_states] => Array
(
[approve] => Array
(
[target] => approved
)
)
)
[approved] => Array
(
[label] => Order approved
[roles] => Array
(
[0] => ROLE_USER
)
[next_states] => Array
(
[order] => Array
(
[target] => ordered
)
)
)
)
How can I merge the arrays so that the $value (which will be either true or false) from the first array can be merged to the second one as a [state] as follows (There are comments at the lines [state]);
Array
(
[created] => Array
(
[label] => Order created
[roles] => Array
(
[0] => ROLE_USER
)
[next_states] => Array
(
[approve] => Array
(
[target] => approved
)
)
[state] => 1 // I want to add this line here from the other array
)
[approved] => Array
(
[label] => Order approved
[roles] => Array
(
[0] => ROLE_USER
)
[next_states] => Array
(
[order] => Array
(
[target] => ordered
)
)
[state] => 1 // I want to add this line here from the other array
)
)
Note that the $keys from the first array and the second array are the same, but I want to add the $values to the arrays that serves as $values in the second array. What will be the best way to do this?
foreach($array1 as $a => $b)
$array2[$a]['state'] = $b;
edit:
I've seen the comment. To set state to false when the right key doesn't exist in the first array, you can use the following code.
foreach($array2 as $a => &$b)
$b['state'] = isset($array1[$a]) && $array1[$a];
You can use array_merge_recursive function. It used merge the array dynamically
DEMO Please click here
Another example

Most efficient way to sort a multidimensional array (php)

I am looking for the most efficient way to sort a multidimensional array in php.I want to sort the array by the value name in [result][index][kl][0]. What is the fastest way to do this? My array has like 100 elements.
My array looks like this:
Array
(
[jsonrpc] => 2.0
[id] => req-002
[result] => Array
(
[0] => Array
(
[type] => subst
[lsid] => 11
[date] => 20150209
[startTime] => 955
[endTime] => 1040
[kl] => Array
(
[0] => Array
(
[id] => 29
[name] => S12UB
)
)
)
[1] => Array
(
[type] => subst
[lsid] => 11
[date] => 20150209
[startTime] => 1045
[endTime] => 1130
[kl] => Array
(
[0] => Array
(
[id] => 29
[name] => S12UB
)
)
)
Thank you.
Use a user-defined sort function, like usort along with strcmp:
function compareByName($a, $b) {
// the strcmp returns a numeric value above or below 0 depending on the comparison
return strcmp($a['k1'][0]['name'], $b['k1'][0]['name']);
}
Then, assuming your multi-dimensional array is named $array, replace the multi-dimensional array $array['result'] with the newly sorted array:
$array['result'] = usort($array['result'], 'compareByName');
The documentation for the usort and strcmp should be self-explanatory for understanding how the code works above.
$result = uasort($result, function($a, $b) {
return strcmp($a['kl'][0]['name'], $b['kl'][0]['name']);
});

sorting an array dynamically without knowing existing dimensions

I know that ksort() is used to sort array by keys. The max dimension of my array always differs. Sometime 2, sometimes 5, sometime 10 dimensional so how do I use ksort() to do sorting dynamically without knowing how many dimensions exist in it?
Thanks
EXAMPLE ARRAY
[2010] => Array
(
[3] => Array
(
[B] => Array
(
[6] => Array
(
[Patato] =>
)
[C] => Array
(
[Patato] =>
[Zozo] =>
)
)
[A] => Array
(
[F] => Array
(
[Tomato] =>
[Apple] =>
[Kiwi] =>
)
)
[1] => Array
(
[4] => Array
(
[A] => Array
(
[Orange] =>
)
)
If you mean you want to sort recursive:
function deep_ksort(&$arr) {
ksort($arr);
foreach ($arr as &$a) {
if (is_array($a) && !empty($a)) {
deep_ksort($a);
}
}
}
This comes from http://www.php.net/manual/en/function.ksort.php#105399

Sorting an array according to a key in multi dimensional array and preserving other key values. PHP

This is my array.
Array
(
[Data] => Array
(
[0] => Array
(
[recipeid] => 108
[recipe] => Rasams- the tongue ticklers !
[image] => No data
[category] => Rasams and Soups
)
[1] => Array
(
[recipeid] => 44
[recipe] => Brain Booster- do you want to try it?
[image] => brain-booster-do-you-44-HP-62.jpg
[category] => Drinks and Smoothies
)
[2] => Array
(
[recipeid] => 36
[recipe] => Pineapple Grape Smoothy--a rare combo
[image] => pineapple-grape-smoo-36-HP-62.jpg
[category] => Drinks and Smoothies
)
)
)
I have to sort [DATA] array according to alphabetical order of [key]recipe's value, also preserve the recipeid, image, category after sorting.
Use usort.
usort($yourarray['Data'], 'data_sort');
function data_sort($a, $b) {
return (strcasecmp($a['recipe'], $b['recipe']) > 0);
}
You should be able to do this with usort(). Here's an example of how it could be done, based off of the example given in the PHP docs.
function cmp($a, $b)
{
if ($a['recipe'] == $b['recipe']) {
return 0;
}
return ($a['recipe'] < $b['recipe']) ? -1 : 1;
}
usort($a, "cmp");

Categories