PHP USort gives null warning when array is empty - php

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
}

Related

Laravel sorting associative arrays

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

How to retrieve the complete array element or index from the multi-dimensional array when its some of the key and values are known

I have an array which consists of arrays. So, now suppose I want to retrieve the sku and price whose
key value is 2=>5 and 3=>7 so it should return price=>13 and sku=>bc i.e. that array whose index is at 1 in the array.
Hi I would probably try the following (Same as Riziers comment)
foreach($array as $key => $item) {
if($item[2] == 5 && $item[3] == 7) {
// return price
return $item;
}
}
There is a function array_search, which does what you want but for simple values. You can define your own function that will take not $needle, but callable predicate:
function array_search_callback(callable $predicate, array $array)
{
foreach ($array as $key => $item) {
if ($predicate($item)) {
return $key;
}
}
return false;
}
Having this function your example can be done like this:
$key = array_search_callback(function ($item) {
return $item[2] === '5' && $item[3] === '7';
}, $array);
$result = $key === false ? null : $array[$key];
I could simply return an item from the search function. But to be consistent with the original search function, I am returning the index.
As array_search_callback takes callable as an argument you can provide any criteria you want without the need of modifying the function itself.
Here is working demo.

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?

Sorting database values using usort and Sortby(collection method)

I am trying to use usort and sortby to basically after the code below is
1. sort $featureLists by 'feature_sort_order'
2. loop over each $featureList and reset sort order to integer a contiguous value
my Petitions table has feature_sort_order column having values 1 to 6
i used this part of code in controller :
$featureList = Petition::getFeaturedPetitions();
$Petition->featured_sort_order = Input::get('dropdown_menu_list');
foreach($featureList as $featureLists)
{
if($featureLists->feature_sort_order >= $Petition->featured_sort_order && $featureLists->id != $Petition->id)
{
$featureLists->feature_sort_order += 1;
}
}
Here what i trying to do is select a value from dropdown menu and to change current item position before selected item.
i have tried few things like this but doesn't work out :
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
function sortBy(&$items, $key) {
if (is_array($items)) {
return usort($items, function($a, $b) use ($key) {
return cmp($a[$key], $b[$key]);
});
}
return false;
}
sortBy($featureList, 'featured_sort_order');
// foreach($featureLists->feature_sort_order as featureOrder)
// {
// }

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']);
}

Categories