Jump to content

A little question about array sorting


roddik

Recommended Posts

Hello everybody! I have a 2-dimensional array, for instance, this one[pre]Array
(
    [0] => Array
        (
            [url] => www.php.net/
            [pr] => 9


        )

    [1] => Array
        (
            [url] => www.php.net/downloads.php
            [pr] => 8


        )

    [2] => Array
        (
            [url] => en.wikipedia.org/wiki/PHP
            [pr] => 7


        )

    [8] => Array
        (
            [url] => www.zend.com/
            [pr] => 8


        )
)[/pre] Is there any built-in php function to sort first-level arrays by 'pr' (to make zend.com appear in the beginning)? Or, if there is not, what is the preferred way to do it? TIA
Link to comment
https://forums.phpfreaks.com/topic/34770-a-little-question-about-array-sorting/
Share on other sites

WOOHOO!!! Thank you, Caesar, now everything works!!! I've even found a php.net example of sorting multi-dimensional array)))) ;D ;D

[code]
<?php
function cmp($a, $b)
{
  return strcmp($a["fruit"], $b["fruit"]);
}

//Example 2. usort() example using multi-dimensional array

$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";

usort($fruits, "cmp");

while (list($key, $value) = each($fruits)) {
  echo "\$fruits[$key]: " . $value["fruit"] . "\n";
}

//The above example will output:

//$fruits[0]: apples
//$fruits[1]: grapes
//$fruits[2]: lemons
?> [/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.