I’ve haven’t found an array function that will let me sort this result by the [‘item_name’] key. If that function exists, what is it please? If not, how should I be thinking to sort these rows by [‘item_name’]?
Thanks for your time.
Here’s the vardump() of my sample array which is called $result:
array(4) { [0]=> string(1) “4” [“id”]=> string(1) “4” [1]=> string(28) "Superboy and His Dog Krypto " [“item_name”]=> string(28) "Superboy and His Dog Krypto " }
array(4) { [0]=> string(1) “5” [“id”]=> string(1) “5” [1]=> string(12) “Wonder Woman” [“item_name”]=> string(12) “Wonder Woman” }
array(4) { [0]=> string(1) “6” [“id”]=> string(1) “6” [1]=> string(16) “Seaview 8-Window” [“item_name”]=> string(16) “Seaview 8-Window” }
array(4) { [0]=> string(1) “7” [“id”]=> string(1) “7” [1]=> string(31) "Creature from the Black Lagoon " [“item_name”]=> string(31) "Creature from the Black Lagoon " }
<?php
/****
* Find more info here: http://us2.php.net/manual/en/function.uasort.php
* I didn't setup the array exactly as you had it but I think you can get
* the idea behind it. By the way it's sorted by array indices.
*/
$result = array(
1 => array("id" => 4, "item_name" => "Superboy and His Dog Kryto"),
2 => array("id" => 5, "item_name" => "Wonder Woman"),
3 => array("id" => 6, "item_name" => "Seaview 8-Window"),
4 => array("id" => 7, "item_name" => "Creature from the Black Lagoon")
);
// Name sorting function:
function name_sort($x, $y) {
return strcasecmp($x['item_name'], $y['item_name']);
}
// Print the array as is:
echo '<h2>Array As Is</h2><pre>' . print_r($result, 1) . '</pre>';
// Sort by name:
uasort($result, 'name_sort');
echo '<h2> Array Sorted by "item_name"</h2><pre>' . print_r($result, 1) . '</pre>';
Thanks.
How would I create a new array in [‘item_name’] order based on your original array?
OK. I didn’t realize uasort() rearranged the array.
Thanks for you help QMonkey
and especially for your’s Strider64 that was a lot of work.