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']);
});
Related
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.
I have a multi-dimensional array that I would like to get unique sub-values from, but also have a count of how many times those unique sub-values occurred.
For instance, this would be my starting array:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 3333333333333333
)
)
[1] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 5555555555555555
)
)
[2] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 77777777777777777
)
)
In the end, I'd like to have an array that looks like this:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
[count] => 3
)
[1] => Array
(
[id] => 3333333333333333
[count] => 1
)
[2] => Array
(
[id] => 5555555555555555
[count] => 1
)
[3] => Array
(
[id] => 77777777777777777
[count] => 1
)
)
Is there any general/easy way to do this without iterating through the first array for each value, comparing/storing the values in a temporary array, checking them, and adding to the count?
To get this exact format you may need to iterate thought your current array and do the counting manually, however php has the array_count_values() and array_unique() functions for this kind of thing:
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.array-unique.php
Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. Note that a reference to the output array $counted is used in the callback.
array_walk_recursive($ids, function($id, $k) use (&$counted) {
$counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});
Using the id as the key in the $counted array will simplify the counting. The result of this will be somewhat different from your suggested output, but in my opinion it would actually be simpler to use. (e.g. foreach ($counted as $id => $count) {...).
$counted = array(
"1533438473619168" => 3
"3333333333333333" => 1
"5555555555555555" => 1
"77777777777777777" => 1);
This is an issue I haven't come across before, and there doesn't appear to be a function for this.
I'm trying to sort the following multi dimensional array by its keys
Array (
[hiphop] => Array (
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
[rnb] => Array (
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
[dubstep] => Array ( )
[reggae] => Array ( )
[trap] => Array ( )
)
From this arrays values
Array (
[0] => hiphop
[1] => dubstep
[2] => reggae
[3] => trap
[4] => rnb
[5] => rnb
)
Has anyone attempted this before or know a workaround?
An explanation would be great as I'm new to multi dimensional arrays
Many thanks in advance if you can help!
The final output would match the value organsiation from the non multi dimensional array like so
Array (
[hiphop] => Array (
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
[dubstep] => Array ( )
[reggae] => Array ( )
[trap] => Array ( )
[rnb] => Array (
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
)
The easiest Way would be to "flip" the array definining the sorting - then grab the values matching the new keys in the other array:
$s = array ("hiphop","dubstep","reggae","trap","rnb");
$target = array_flip($s);
foreach($target AS $key => &$value){
$value = $array_containing_unsorted_values[$key];
}
Note: does not work if the same value appears twice in the sorting array - but that never should happen, cause does not make sence for a sorting-definition.
Should be way faster than using array_search 2 times within each sorting comparission.
result:
Array
(
[hiphop] => Array
(
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
[dubstep] => Array
(
)
[reggae] => Array
(
)
[trap] => Array
(
)
[rnb] => Array
(
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
)
You can use uksort for that, which lets you sort by key, based on a user supplied comparison function. Using that function, you can make a function yourself, so you can hide the complex functionality in an easy one.
For instance, you want a function like ksort, only you want to specify an external array of keys, so such a function could look like this:
bool ksort_ext(&$array, $keys)
For the implementation, you can wrap uksort, like so:
function ksort_ext(&$array, $keys)
{
return uksort($array, function($a, $b) use ($keys) {
// Result of callback is based on the position of $a and $b in $keys
return array_search($a, $keys) - array_search($b, $keys);
});
}
Then, you can call it like you would call other sort functions, only with the external array of keys as a second parameter.
$array1 = array(
'hiphop' => array(1,2,3),
'rnb' => array(1,2,3),
'dubstep' => array(1,2,3),
'reggae' => array(1,2,3),
'trap' => array(1,2,3));
$array2 = array('hiphop', 'dubstep', 'reggae', 'trap', 'rnb');
var_dump(ksort_ext($array1, $array2)); // Should return true
var_dump($array1); // Should display the ordered array,
I need to sort my array php by a key value. my array:
Array
(
[1430039342393636453] => Array
(
[0] => Array
(
[thrid] => 1430039342393636453
[uid] => 19748
[flag] => 1
[timestamp] => 1363791789
[date] => Mar 20
[content_preview] =>
[content] =>
)
)
[1430750471744336569] => Array
(
[0] => Array
(
[thrid] => 1430750471744336569
[uid] => 19870
[flag] => 1
[timestamp] => 1364469959
[date] => Mar 28
[content_preview] =>
[content] =>
)
[1] => Array
(
[thrid] => 1430750471744336569
[uid] => 19874
[flag] => 1
[timestamp] => 1364472417
[date] => Mar 28
[content_preview] =>
[content] =>
)
)
I need to sort by timestamp the main array and also the childs arrays.
Any suggesitons?
Use asort to sort associative arrays.
Related S.O. Post:
Sorting an associative array in PHP
Try something like:
private function sort($sort) {
foreach($sort as &$arr) { //use a reference because usort uses references to manipulate your array.
//if you don't pass by reference you'll never see the sort
usort($arr, function($a, $b) { return $a['timestamp'] - $b['timestamp'];
}
usort($sort, function($a, $b) { return key($a) - key($b); }
return $sort;
}
Hi i have an array which is combinations of 2 separate array producing same result, following are my array after merge :
Array
(
[0] => Array
(
[event_id] => 16
[user_id] => 3
[date_created] => 20130227095010
)
[1] => Array
(
[event_id] => 15
[user_id] => 2
[date_created] => 20130227101734
)
[2] => Array
(
[event_id] => 16
[user_id] => 2
[date_created] => 20130227094856
)
)
From here onwards how can i sort again based on date_created desc or asc without using previously 2 arrays which i used to merge?
Update:
previous array result
array1:
Array
(
[0] => Array
(
[event_id] => 15
[user_id] => 2
[date_created] => 20130227101734
)
[1] => Array
(
[event_id] => 16
[user_id] => 2
[date_created] => 20130227094856
)
)
array 2:
Array
(
[0] => Array
(
[event_id] => 16
[user_id] => 3
[date_created] => 20130227095010
)
)
How can i use loop within these function to extract date_created for each of above arrays
usort($array,function($a, $b) { return $a['date_created'] - $b['date_created']; });
Very easy with something like usort().
usort($array,
function($a, $b) { return $a['date_created'] - $b['date_created']; });
You can create your own sort function callback and use it with the usort method.
There is need to re-use your first 2 arrays.
usort($yourMergedArray, yourCallBackSort);
and
function yourCallBackSort($firstEntry, $secondEntry)
{
// Do some stuff to compare your values and return -1, 0 or 1
// depend if $firstEntry id <, = or > to $secondEntry
}
Try this,
$sort = array();
foreach($your_combined_array as $k=>$v) {
$sort['date_created'][$k] = $v['date_created'];
}
array_multisort($sort['date_created'], SORT_DESC, $your_combined_array);
echo "<pre>";
print_r($your_combined_array);