[ create a new paste ] login | about

Link: http://codepad.org/DxgFznzE    [ raw code | output | fork ]

PHP, pasted on Mar 7:
<?php
//save the values you used 
$fruit = 'Orange';
$color = 'orange';
$shape = 'round';
$price = '£.6';
$array[] = array($fruit, $color, $shape, $price);

$fruit = 'Mango';
$color = 'yellow';
$shape = 'oval';
$price = '£.9';
$array[] = array($fruit, $color, $shape, $price);

$fruit = 'Apple';
$color = 'red ';
$shape = 'round ';
$price = '£.4';
$array[] = array($fruit, $color, $shape, $price);

//display the initial array 
print_r($array);

/**
* define comparing function; it will be used to compare two elements of the array in the process of sorting it 
*/
function my_cmp_func($a, $b)
{
	return strcmp($a[3], $b[3]);//the price is on the key 3 in your arrays
}//end function my_cmp_func

usort($array, 'my_cmp_func');//call usort (or uasort if you want to keep the initial keys) using the function defined above as a parameter

print_r($array);//display the final array


Output:
Array
(
    [0] => Array
        (
            [0] => Orange
            [1] => orange
            [2] => round
            [3] => £.6
        )

    [1] => Array
        (
            [0] => Mango
            [1] => yellow
            [2] => oval
            [3] => £.9
        )

    [2] => Array
        (
            [0] => Apple
            [1] => red 
            [2] => round 
            [3] => £.4
        )

)
Array
(
    [0] => Array
        (
            [0] => Apple
            [1] => red 
            [2] => round 
            [3] => £.4
        )

    [1] => Array
        (
            [0] => Orange
            [1] => orange
            [2] => round
            [3] => £.6
        )

    [2] => Array
        (
            [0] => Mango
            [1] => yellow
            [2] => oval
            [3] => £.9
        )

)


Create a new paste based on this one


Comments: