PHP array sort using inner val - php

Array
(
[1] => Array
(
[id] => 1
[sort] => 1
)
[3] => Array
(
[id] => 3
[sort] => 3
)
[2] => Array
(
[id] => 2
[sort] => 2
)
)
How do i sort it so its re-ordered using the inner 'sort' key ? ie the above would look like this:
Array
(
[1] => Array
(
[id] => 1
[sort] => 1
)
[2] => Array
(
[id] => 2
[sort] => 2
)
[3] => Array
(
[id] => 3
[sort] => 3
)
)

You can use usort with this comparison function:
function cmpBySort($a, $b) {
return $a['sort'] - $b['sort'];
}
usort($arr, 'cmpBySort');
Or you use array_multisort with an additional array of key values for the sort order:
$keys = array_map(function($val) { return $val['sort']; }, $arr);
array_multisort($keys, $arr);
Here array_map with the anonymous function is used to build an array of the sort values that is used to sort the array values itself. The advantage of this is that there is np comparison function that needs to be called for each pair of values.

Something like this:
usort($array, function (array $a, array $b) { return $a["sort"] - $b["sort"]; });

Something like this:
uasort($array, 'compfunc');
function compfunc($a, $b)
{
return $a['sort'] - $b['sort'];
}

Related

How to sort array based on subarray value in PHP [duplicate]

This question already has answers here:
PHP Sort Array By SubArray Value
(8 answers)
Closed 3 years ago.
I have array like this
Array
(
[0] => Array
(
[id] => 16059
[product_id] => 4013
[Product] => Array
(
[id] => 4013
[name] => XYZ
)
)
[1] => Array
(
[id] => 16060
[product_id] => 4462
[Product] => Array
(
[id] => 4462
[name] => MNOP
)
)
[2] => Array
(
[id] => 16061
[product_id] => 4473
[Product] => Array
(
[id] => 4473
[name] => ABCD
)
)
)
How to short this array using Product > name in ascending order. I can do using for-each loop, but there is any method to without loop ?
Use usort() with strcmp():
usort($array, function($a, $b) {
return strcmp($a['Product']['name'] , $b['Product']['name']);
});
print_r($array);
Output:- https://3v4l.org/Cb5S5
Try -
usort($array, function($a, $b) {
return $a['Product']['name'] > $b['Product']['name'];
});
usort()
Here is the snippet,
$t = [];
foreach ($arr as $key => $value) {
$t[$key] = $value['Product']['name'];
}
array_multisort($t, SORT_ASC, $arr);
First, fetch the data of that name and create an array.
Pass the relevant array for sorting criteria to a multidimensional array.
Demo.

how can I get sorting multidimnsion array by value?

How can I sorting multidimension array by value?
Example my output array like this :
[0] => Array
(
[0] => 2
[1] => 5
[2] => 8
[3] => 1
)
[1] => Array
(
[0] => 7
[1] => 4
[2] => 1
)
Can I get sorted array like this?
[0] => Array
(
[3] => 1
[0] => 2
[1] => 5
[2] => 8
)
[1] => Array
(
[2] => 1
[1] => 4
[0] => 7
)
Thanks..
simple way with asort
array_map(function($v){asort($v); return $v;}, $array);
Or you can use uasort()
You can use uasort and array_map to sort the elements. for php7+
array_map(function($v){uasort($v, function($a, $b){return $a <=> $b;}); return $v;}, $array);
for version<7 use this compare funciton of uasort
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
You can use array_walk together with asort:
array_walk($array, 'asort');
Here is working demo.
You can use arsort to sort array by values. make a loop and call asort($arr) for each sub array you have

How to intersect Array of objects in PHP?

How do i get the result of intersection between two Array of Objects in PHP.
For Example,
the value of $array1 is
Array
(
[0] => stdClass Object
(
[id] => 2
[influencer_id] => 2
[follower_id] => 1
)
)
and the value of $array2 is,
Array
(
[0] => stdClass Object
(
[id] => 2
[influencer_id] => 1
[follower_id] => 2
),
[1] => stdClass Object
(
[id] => 3
[influencer_id] => 3
[follower_id] => 2
),
)
So, what i want to get in $result is
Array
(
[0] => stdClass Object
(
[id] => 2
[influencer_id] => 2
[follower_id] => 1
)
)
What is the best way to get it?
Thanks in advance!
You can do that using array_uintersect function and defining manually your callback comparison function :
$arr1 = json_decode('[{"id":2,"influencer_id":2,"follower_id":1}]');
$arr2 = json_decode('[{"id":2,"influencer_id":2,"follower_id":1},{"id":3,"influencer_id":3,"follower_id":2}]');
$arr3 = array_uintersect($arr1, $arr2, function ($e1, $e2) {
if($e1->id == $e2->id && $e1->influencer_id == $e2->influencer_id && $e1->follower_id == $e2->follower_id) {
return 0;
} else {
return 1;
}
});
var_dump($arr3);
Try to use array_intersect
array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.

Php array sort by date after merge

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

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