1

How to sort following array with PHP based on its name's prefix number and produce the output as

    1-First
    2-Capico
    4-Apple
    10-Zebra



$array  = Array ( [0] => stdClass Object ( [term_id] => 6 [name] => 1-First [slug] => aaa-a [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => category [description] => [parent] => 4 [count] => 2 [cat_ID] => 6 [category_count] => 2 [category_description] => [cat_name] => 1-First [category_nicename] => aaa-a [category_parent] => 4 ) 
        [1] => stdClass Object ( [term_id] => 9 [name] => 10-Zebra [slug] => aaa-f [term_group] => 0 [term_taxonomy_id] => 9 [taxonomy] => category [description] => [parent] => 4 [count] => 1 [cat_ID] => 9 [category_count] => 1 [category_description] => [cat_name] => 10-Zebra [category_nicename] => aaa-f [category_parent] => 4 ) 
        [2] => stdClass Object ( [term_id] => 8 [name] => 2-Capico [slug] => aaa-c [term_group] => 0 [term_taxonomy_id] => 8 [taxonomy] => category [description] => [parent] => 4 [count] => 1 [cat_ID] => 8 [category_count] => 1 [category_description] => [cat_name] => 2-Capico [category_nicename] => aaa-c [category_parent] => 4 ) 
        [3] => stdClass Object ( [term_id] => 7 [name] => 4-Apple [slug] => aaa-b [term_group] => 0 [term_taxonomy_id] => 7 [taxonomy] => category [description] => [parent] => 4 [count] => 1 [cat_ID] => 7 [category_count] => 1 [category_description] => [cat_name] => 4-Apple [category_nicename] => aaa-b [category_parent] => 4 ) ) 

Tried using

sort($array,SORT_NUMERIC);
foreach ( $array as $single ) {
echo $single->name;
}

but display the output as

4-Apple
2-Capico
10-Zebra
1-First
0

3 Answers 3

3
usort($array, function($a, $b) 
    { 
        return strnatcasecmp($a->name, $b->name); 
    });`
Sign up to request clarification or add additional context in comments.

Comments

3

To sort numerically:

usort($data, function($a, $b) { return $a->name - $b->name; });

2 Comments

Thanks for all of your response. let me try
@stereofrog, depends on the context. e.g., 1-foo and 1-bar will be considered equal when sorting by integers. But if the prefix index is unique, then it doesn't matter.
3

From PHP 5.3, you can use usort() with an anonymous function. On older versions, you can use a normal compare function as illustrated in the manual.

usort($array, function ($a, $b) {
    //saving values converted to integer
    $intAname=(int) $a->name;
    $intBname=(int) $b->name;

    //if they are equal, do string comparison
    if ($intAname == $intBname) {
        return strcmp($a->name, $b->name);
    }

    return ($intAname < $intBname) ? -1 : 1;
});

Sort order will be fine because your name strings start with numbers, so PHP will parse these variables as numbers.

UPDATE: Added integer casting as suggested by @binaryLV, and also solved the 1-foo and 1-bar problem mentioned by @konforce (with strcmp()).

8 Comments

"2-Capico" > "10-Zebra". I would add (int) before operands.
"Sort order will be fine because (...)" - did you try that? var_dump('2-Capico' > '10-Zebra') prints boolean true. If both strings would be numeric (rather than start with digits), then your statement would be true.
@stereofrog It won't give the right results. Now I see strnatcasecmp() used by @VolkerK, man, I did not know that one :).
@binaryLV Currently I cannot test it, but whenever I can, I will update.
That link to the manual describes how strings are converted to numbers, when such conversion is necessary. If both operands are strings and at least one of them is not numeric, then operands are not converted to numbers. To ilustrate that, var_dump('2' == '0x2', '2' == '0x2x'); will output true, false, as in first case both strings are numeric, in second case second operand is not numeric.
|

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.