0

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..

$keys = array_keys($eventinfo["message"]);
for ($i = 0; $i < count($keys); $i++) {
    $key = ($keys[$i]);
    $perNode = $eventinfo["message"][$key];
    $arrTmp = explode("\n", $perNode);
    $t = count($arrTmp);
    $tmp = 0;
    while ($t) {
        $t--;
        if ($arrTmp[$tmp] != "") {
            $cumltvArray[] = "<a href='#'>" . $key. "</a> : </br>".$arrTmp[$tmp];
        }
        $tmp++;
    }
}
$result_str = events_print($cumltvArray);
// $cumltvArray is the one which I am passing to events_print
4
  • 1
    nothing is clear.... Commented Nov 24, 2014 at 6:47
  • 1
    Please provide your array format Commented Nov 24, 2014 at 6:49
  • $keys= (array_keys($eventinfo["message"])); for ( $i = 0; $i < count($keys); $i++ ){ $key = ($keys[$i]); $perNode= $eventinfo["message"][$key]; $arrTmp=explode("\n",$perNode); $t=count($arrTmp); $tmp = 0; while($t){ $t--; if($arrTmp[$tmp]!=""){ $cumltvArray[] ="<a href='#'>". $key."</a> : </br>".$arrTmp[$tmp]; } $tmp++; } } $result_str = events_print($cumltvArray); $cumltvArray is the one which I am passing to events_print Commented Nov 24, 2014 at 7:08
  • This question is not a good fit for Stack Overflow because it is Unclear and doesn't contain a minimal reproducible example. Commented Oct 9, 2023 at 3:58

2 Answers 2

1

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];
});
Sign up to request clarification or add additional context in comments.

3 Comments

Jack, could you explain the syntaxt of the last part of your code? The usort part. Never seen PHP written this way. What does function($a,$b) do? How does it this work? I also dont understand your return statement. Thanks in advance :)
@Bolli I'm using anonymous functions.
Thanks Jack stackoverflow.com/users/1338292/ja%cd%a2ck nice answer , can it be sorted with the time stamp contains in the line aswell with same complexity with recent time first in order of CRITICAL-->Warning-->INFO
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.