Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

uasort 1

Status
Not open for further replies.

DeepBlerg

Technical User
Joined
Jan 13, 2001
Messages
224
Location
AU
Hi,

I'm having problems with uasort. I have a shopping cart class in a session array like this:

Code:
shopping_cart Object
(
    [_cart] => Array
        (
            [0] => Array
                (
                    [ProductID] => 21
                    [SupplierID] => 2
                    [quantity] => 5
                )

            [1] => Array
                (
                    [ProductID] => 18
                    [SupplierID] => 4
                    [quantity] => 1
                )

            [2] => Array
                (
                    [ProductID] => 22
                    [SupplierID] => 2
                    [quantity] => 2
                )

        )
)

And I want to use uasort to sort this array by SupplierID so the order of the _cart keys would be 0, 2, 1.

Here's my code for the uasort:

Code:
	function by_supplierid($a, $b) {
		if ($a["SupplierID"] == $b["SupplierID"]) {
			return 0;
		}
		return ($a[&quot;SupplierID&quot;] < $b[&quot;SupplierID&quot;]) ? -1 : 1;
	}
	
	function get_items() {
		// return all the items
		uasort($this->_cart, &quot;by_supplierid&quot;);
		return($this->_cart);
	}

Any ideas why it's not sorting?
 
A modified version of your posted code works for me:

Code:
<?php

function by_supplierid($a, $b)
{
	if ($a[&quot;SupplierID&quot;] == $b[&quot;SupplierID&quot;])
	{
		return 0;
	}
	return ($a[&quot;SupplierID&quot;] < $b[&quot;SupplierID&quot;]) ? -1 : 1;
}

$_cart = array
(
	0 => array
	(
		'ProductID' => 21,
		'SupplierID' => 2,
		'quantity' => 5,
	),
	1 => array
	(
		'ProductID' => 18,
		'SupplierID' => 4,
		'quantity' => 1,
	),
	2 => array
	(
		'ProductID' => 22,
		'SupplierID' => 2,
		'quantity' => 2,
	)
);

print '<pre>';
print_r ($_cart);
uasort($_cart, &quot;by_supplierid&quot;);
print &quot;\n&quot;;
print_r ($_cart);
?>

Produces:

Code:
Array
(
    [0] => Array
        (
            [ProductID] => 21
            [SupplierID] => 2
            [quantity] => 5
        )

    [1] => Array
        (
            [ProductID] => 18
            [SupplierID] => 4
            [quantity] => 1
        )

    [2] => Array
        (
            [ProductID] => 22
            [SupplierID] => 2
            [quantity] => 2
        )

)

Array
(
    [0] => Array
        (
            [ProductID] => 21
            [SupplierID] => 2
            [quantity] => 5
        )

    [1] => Array
        (
            [ProductID] => 22
            [SupplierID] => 2
            [quantity] => 2
        )

    [2] => Array
        (
            [ProductID] => 18
            [SupplierID] => 4
            [quantity] => 1
        )

)

Which seems to be what you are describing.

However, uasort() preserves key/value assignments. If you use a for() loop to fetch the values, they'll come out in the old order. A foreach() loop will fetch the values correctly.

If you don't way to keep key/value assignments as they are, use usort().

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top