Laravel sorting associative arrays - php

I am trying to sort an array based on a specific key but it's not working. The array is below when in JSON format. I want to sort it in ascending order by id_question.
This is what I have done so far:
public function compare($ar1, $ar2){
if ($ar1['id_question']<$ar2['id_question']) {
return 1;
}else {
return -1;
}
}
Call the sort function:
uasort($related, Array ($this, 'compare'));
This is what it returns:
As you can see, it doesn't apply the sort.
It's done
here is solution
usort($related, function($a, $b){
if ($a['id_question'] < $b['id_question']) {
return -1;
}else {
return 1;
}
});

I hope this helps -
$listItem = collect($related)->sortBy('id_question')->toArray();

Please try:
$related = collect($related)->sortBy('id_question')->all();

Related

Sort array by value of a specific key [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
I've got an array as such:
$currentTerm = array(
'name' => $term->name,
'weight' => $term->weight
);
I'd like to sort the entire array by the weight key. I can't figure out how to accomplish this. Any thoughts or should I write a custom function for this?
P.S Some weights can be the same; so storing them as a key isn't an option. And it's multidimensional with a children key which is filled recursively the same way as above.
You can create a custom sorting method usort().
It would work similarly to this:
usort($array, function($a,$b) {
if($a->weight === $b->weight) return 0;
return ($a->weight < $b->weight) ? -1 : 1;
});
If people from the PHP7 future come here, they can just use this (spaceships!):
usort($array, function($a,$b) {
return $a->weight <=> $b->weight;
});
A full (and cumbersome) example for pre-PHP7 would be:
<?php
class Obj {
public $name;
public $weight;
function __construct($name,$weight) {
$this->name = $name;
$this->weight = $weight;
}
}
$array = array(
new Obj("A",3),
new Obj("B",10),
new Obj("C",8),
);
usort($array, function($a,$b) {
if($a->weight == $b->weight) return 0;
return ($a->weight < $b->weight) ? -1 : 1;
});
print_r($array);
?>
Take a look at array_multisort()
$weight = array();
foreach ($currentTerm as $key => $row)
{
$weight[$key] = $row['weight'];
}
then
array_multisort($weight, SORT_ASC, $currentTerm);
or
array_multisort($weight, SORT_DESC, $currentTerm);
You can try this code:
//define a comparison function
function cmp($a, $b) {
if ($a['status'] == $b['status']) {
return 0;
}
return ($a['status'] < $b['status']) ? -1 : 1;
}
usort($array, "cmp");

PHP USort gives null warning when array is empty

function sort_multi_array($array, $key)
{
if (is_null($array)) return 0;
$keys = array();
for ($i=1;$i<func_num_args();$i++) {
$keys[$i-1] = func_get_arg($i);
}
// create a custom search function to pass to usort
$func = function ($a, $b) use ($keys) {
for ($i=0;$i<count($keys);$i++) {
if ($a[$keys[$i]] != $b[$keys[$i]]) {
return ($a[$keys[$i]] > $b[$keys[$i]]) ? -1 : 1;
}
}
return 0;
};
usort($array, $func);
return $array;
}
I'm building a simple search query however when it reaches the end i.e. no more entries in Warning: usort() expects parameter 1 to be array, null given in
How can I test to see if the array is empty and simply return a null result before it reaches the usort line?
thank you!
Check before using usort if the $array is null or not.
if ($array !== NULL) {
usort($array, $func);
}
Add a check for empty data -
if (!empty($array)) {
// process data
}

Sort PHP array of string (line) with two substring

I wanted to sort php array based on CRITICAL , WARNING ,INFO sub string and then CRITICAL , WARNING ,INFO sub array should be sorted again with the time stamp value contains in each line of string in acsending order.
Basically at the end I need array to be sorted with CRITICAL 1st with time stamp sorted then WARNING and then INFO so on..
First, define a function that turns the urgency of a line into a number.
function urgency($line)
{
if (strpos($line, 'INFO') !== false) {
return 1;
} elseif (strpos($line, 'WARNING') !== false) {
return 2;
} elseif (strpos($line, 'CRITICAL') !== false) {
return 3;
}
return 0;
}
Then, assuming each element of your array contains a line of the file, you need to apply a decorator to keep the sort stable; see also my earlier answer on the subject:
array_walk($array, function(&$element, $index) {
$element = array($element, $index); // decorate
});
After applying the decorator, you sort the array; I'm using a stable comparison helper:
function stablecmp($fn)
{
return function($a, $b) use ($fn) {
if (($tmp = call_user_func($fn, $a[0], $b[0])) != 0) {
return $tmp;
} else {
return $a[1] - $b[1];
}
};
}
usort($array, stablecmp(function($a, $b) {
return urgency($b) - urgency($a);
}));
Finally, undecorate the array to produce the end result:
array_walk($array, function(&$element) {
$element = $element[0];
});
Getting CRITICAL on the sorted order
function my_cmp($a, $b){
$pieces_a = explode("CRITICAL", $a);
$pieces_b = explode("CRITICAL", $b);
if(!isset($pieces_a[1]) && isset($pieces_b[1])) {
return 1;
}
elseif(!isset($pieces_b[1]) && isset($pieces_a[1])) {
return -1;
}
elseif(!isset($pieces_a[1]) && !isset($pieces_b[1])) {
return 0;
}
return strcasecmp($pieces_a[1], $pieces_b[1]);
}
usort($arr, "my_cmp");
But this can only sort if the each line has non spaces I mean single word,.
Any other solution curious to know?

Sort multi-dimensional array with usort

The following usort function does not always give the right result since it will only "push" up or down one position relative to the compared item. Thus when performing the sort multiple times the result Yes No Yes Nocan occur.
The function successfully sort field b.
How can I solve this?
array
[0] => array("a"=>"Yes","b"=>"apple"...);
[1] => array("a"=>"Yes","b"=>"banana"...);
[2] => array("a"=>"No","b"=>"lemon"...);
[3] => array("a"=>"No","b"=>"grape"...);
...
current function
function sortAnserDesc($x, $y){
if ( $x['a'] == $y['a'] )
return 0;
else if ( $x['a'] < $y['a'] )
return 1;
else
return -1;
}
I found a very good function on http://www.php.net/manual/en/function.usort.php#103722
And I think you have a general problem.
I try to display the callback function for usort here.
function usortByArrayKey($key, $asc=SORT_ASC) {
$sort_flags = array(SORT_ASC, SORT_DESC);
if (!in_array($asc, $sort_flags))
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
return function(array $a, array $b) use ($key, $asc, $sort_flags) {
if (!is_array($key)) { //just one key and sort direction
if (!isset($a[$key]) || !isset($b[$key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$key] == $b[$key])
return 0;
return ($asc == SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
} else { //using multiple keys for sort and sub-sort
foreach ($key as $sub_key => $sub_asc) {
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if (!in_array($sub_asc, $sort_flags)) {
$sub_key = $sub_asc;
$sub_asc = $asc;
}
//just like above, except 'continue' in place of return 0
if (!isset($a[$sub_key]) || !isset($b[$sub_key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$sub_key] == $b[$sub_key])
continue;
return ($sub_asc == SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
};
}
And to integrate with your code, you might have something like:
Sorting a value only by DESCENDING.
usort($YOUR_ARRAY, usortByArrayKey('a', SORT_DESC));
Sorting a and b.
usort($YOUR_ARRAY, usortByArrayKey(array('a', 'b')));
More on sorting a and b by DESCENDING
usort($YOUR_ARRAY, usortByArrayKey(array('a', 'b')), SORT_DESC);
Hope this help!!
Why don't you directly use strcmp function?
function sortAnserDesc($x, $y){
return strcmp($x['a'],$y['a']);
}

How to merge and asort() two object arrays into one new array?

function sortnmerge($array1,$array2)
$all= array_merge_recursive($array1, $array2);
asort($all);
While this works for regular arrays, is there a way to apply it to two array of objects or more?
$all would be the result of merging both contact lists.
Thank you in advance,
JP-
<?php
function arrayobj_merge_recursive2($paArray1, $paArray2)
{
if(is_array($paArray2))
{
foreach ($paArray2 AS $sKey2 => $sValue2)
{
$paArray1[$sKey2] = arrayobj_merge_recursive2(#$paArray1[$sKey2], $sValue2);
}
}
elseif(is_object($paArray2))
{
foreach ($paArray2 AS $sKey2 => $sValue2)
{
$paArray1->{$sKey2} = arrayobj_merge_recursive2(#$paArray1->{$sKey2}, $sValue2);
}
} else {
return $paArray2;
}
return $paArray1;
}
?>
Two steps: define a function to compare the objects, then call usort($all, "cmp"). Example:
<?php
function cmp($a, $b) {
return $a->some_value > $b->some_value;
}
function sortnmerge($arr1, $arr2) {
$all = array_merge_recursive($arr1, $arr2);
usort($all, "cmp");
}
?>
I assume, of course, that you're trying to merge two arrays of objects and then compare them based on some property.

Categories