Sorting an assosiative array

Mr_Splash
Mr_Splash used Ask the Experts™
on
Hi,

I have an array that's structed like the example below. Can anyone tell me how I can order it by the "Fruit" value.
array(
 
  [0] => array("Furniture"=>"Table", "Fruit"=>"Banana","Shoes"=>"Brogues"),
  [1] => array("Furniture"=>"Chair", "Fruit"=>"Apple","Shoes"=>"Wellingtons"),
  [2] => array("Furniture"=>"Bookcase", "Fruit"=>"Guava","Shoes"=>"Trainers"),
  [3] => array("Furniture"=>"Fridge", "Fruit"=>"Orange","Shoes"=>"Sandals")
 
);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Why is the array structured that way?  Why not structure it so you can order it easily?

Author

Commented:
The data has come from a MySQL database.

If it's not possible like this, how would this be better sructured?
Most Valuable Expert 2011
Top Expert 2016

Commented:
I think we can sort it.  Let me test a script and I will post a sample in a moment.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Most Valuable Expert 2011
Top Expert 2016

Commented:
This example will leave associative indexes in the $new array.  See if it makes sense and please post back here with any question.  Best regards, ~Ray
<?php // RAY_sort_array.php
error_reporting(E_ALL);
echo "<pre>\n"; // READABILITY
 
 
// FROM THE OP
$arr = array(
 
  0 => array("Furniture"=>"Table",    "Fruit"=>"Banana", "Shoes"=>"Brogues"),
  1 => array("Furniture"=>"Chair",    "Fruit"=>"Apple",  "Shoes"=>"Wellingtons"),
  2 => array("Furniture"=>"Bookcase", "Fruit"=>"Guava",  "Shoes"=>"Trainers"),
  3 => array("Furniture"=>"Fridge",   "Fruit"=>"Orange", "Shoes"=>"Sandals")
 
);
 
// VISUALIZE THE INPUT
// var_dump($arr);
 
// COPY THE ARRAY
$new = array();
foreach ($arr as $key => $sub_array)
{
    $fruit = $sub_array["Fruit"];
    $new["$fruit"] = $sub_array;
}
 
// VISUALIZE THE COPY
// var_dump($new);
 
// SORT THE COPY
ksort($new);
 
// VISUALIZE THE SORTED COPY
var_dump($new);

Open in new window

Author

Commented:
Thank you Ray,

However, this won't work if two sub arrays have the same fruit. Sorry I should have made that clearer.
Most Valuable Expert 2011
Top Expert 2016

Commented:
"Sorry I should have made that clearer."

Yes, and if that was something you needed, it would have been good to post an example that showed what you needed!  

What would be the criteria for two "Guava" - which should come first?  These are app development and business logic choices that you need to clear up before you write the code.
Most Valuable Expert 2011
Top Expert 2016
Commented:
This will let you preserve the original order in the case of duplicate fruits.  But as I said, the subsidiary order is a business rules questino.

HTH, ~Ray
<?php // RAY_sort_array.php
error_reporting(E_ALL);
echo "<pre>\n"; // READABILITY
 
// HOW TO SORT THIS MULTIDIMENSIONAL ARRAY ON THE "Fruit" ELEMENT
 
// FROM THE OP
$arr = array(
  0 => array("Furniture"=>"Table",    "Fruit"=>"Banana", "Shoes"=>"Brogues"),
  1 => array("Furniture"=>"Chair",    "Fruit"=>"Apple",  "Shoes"=>"Wellingtons"),
  2 => array("Furniture"=>"Bookcase", "Fruit"=>"Guava",  "Shoes"=>"Trainers"),
  3 => array("Furniture"=>"Bed",      "Fruit"=>"Guava",  "Shoes"=>"Socks"),
  4 => array("Furniture"=>"Fridge",   "Fruit"=>"Orange", "Shoes"=>"Sandals")
);
 
// VISUALIZE THE INPUT
// var_dump($arr);
 
// COPY THE ARRAY EXTRACTING THE FRUIT AND ORIGINAL POSITION TO USE AS A KEY
$new = array();
foreach ($arr as $key => $sub_array)
{
    $fruit = $sub_array["Fruit"];
    $newkey = "$fruit" . "$key";
    $new["$newkey"] = $sub_array;
}
 
// VISUALIZE THE COPY
// var_dump($new);
 
// SORT THE COPY
ksort($new);
 
// VISUALIZE THE SORTED COPY
var_dump($new);

Open in new window

Author

Commented:
Perfect, the array is already in its subsidiary order. This was a basic example, in the app it's already in Surname order.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Excellent!  Thanks for the points - it's a good question.

If you wanted to sort in (for example) Fruit + Shoes order you might make that adjustment on lines 23 and 24 as you construct the $newkey variable.

Best,  ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial