Sort PHP array of string (line) with two substring - php

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?

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

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
}

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)
// {
// }

How to return control from callback function or break the processing of array in middle array_filter processing

Can we break execution of callback once condition satisfied with one element of array?
ex .
$a = array(1,2,3,4,5);
foreach($a as $val){
if ($val == 3){
break;
}
}
if we write call back for it, it will be as below
$result = array_filter($a, function(){
if ($val == 3){
return true;
}
});
In callback it will go through all array element, in spite of condition is being satisfied at 3. rest two elements 4, 5 will also go through callback
I want such function in callback, which will break callback one desired condition match and stop execution of rest of elements
Is is possible?
You can do that with a static variable. A static variable is of local scope inside the callback function but preserves its value between calls.
It behaves like a global variable in terms of its value, but with a local scope:
$callback = function($val)
{
static $filter = false;
if ($val == 3) {
$filter = true;
}
return $filter;
};
This callback will return false until $val == 3. It then will return true.
I dont think you can achieve this with array_filter, but you can do something like this:
$a = array(1,2,3,4,5);
try {
array_walk($a, function($value, $key) use(&$a) {
if ($value == 3){
throw new Exception("condition match");
}
unset($a[$key]);
});
}
catch(Exception $e) { }
var_dump($a);

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