0

I want to sort the below array using php. The array should sort by value of services [0][1]...

Array
(
[Services] => Array
    (
        [0] => Travel & Hotels  ~72
        [1] => Automotive~119
        [3] => Professional Services~71
        [4] => Home Services~70
        [6] => IT Services & Computer Repair~83
        [8] => Health and Medical~69
    )

[Restaurants] => Array
    (
        [0] => American / Eclectic~2
        [1] => Cafe~11
        [2] => Desserts & Bakery~111
    )

[Shopping] => Array
    (
        [0] => Office Equipment~103
        [3] => Department Stores~91
        [15] => Cosmetics & Beauty Supply~90
        [16] => Musical Instruments & Teachers~102
        [17] => Food~87
    )

[Entertainment] => Array
    (
        [0] => Bars/Pubs~48
    )

[Travel] => Array
    (
        [0] => Hotels~129
    )
)
larsAnders
  • 3,813
  • 1
  • 15
  • 19
Arun
  • 33
  • 1
  • 6
  • i am new to this can you please help me i tried usort but its not working – Arun Mar 14 '14 at 13:32
  • 1
    If you tried, then, please, show your approach – Alma Do Mar 14 '14 at 13:33
  • i am storing this array in $temp. so I tried like usort($temp); i am not have any idea on this please help me – Arun Mar 14 '14 at 13:35
  • Probably similar question is answered here: http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value –  Mar 14 '14 at 13:35
  • Could you explain exactly what you mean by "The array should sort by value of services [0][1]…" – Patrick Q Mar 14 '14 at 13:36
  • @PatrickQ the sorting should done inside service,restaurants,shopping and etc that is inside service automotive,home service and etc should be sorted – Arun Mar 14 '14 at 13:38
  • Yes, but what is your definition of sorting? Alphabetic? A-Z? Z-A? Do you want to maintain indexes? – Patrick Q Mar 14 '14 at 13:41
  • @PatrickQ Alphabetic A-Z and array format should remain same – Arun Mar 14 '14 at 13:44

1 Answers1

0

It should be as simple as doing this (assuming that $attractionArray is the outer array given above):

foreach($attractionsArray as $key => $innerArray)
{
    sort($innerArray, SORT_STRING);
    $attractionArray[$key] = $innerArray;
}

See it in action here

Patrick Q
  • 6,373
  • 2
  • 25
  • 34