1

I have a multidimensional array:

$externalData=array((array("a","b",3,"d"),array("f","g",1,"h),...))

I want to sort the arrays inside $externalData based on the numeric index (in this case at index 2, so the second array should come first once $externalData is sorted).

Is this possible? How? Thanks in advance, any help is appreciated

  • can you share expected result as is not quite clear what are you trying to achieve? – mitkosoft Mar 23 '16 at 07:38
  • In the case above: array("f","g",1,"h),array("a","b",3,"d") because at index 2 there's 1 and at index 0 there's 3 –  Mar 23 '16 at 07:41
  • @GeoffAtkins: indeed it is, sorry, I haven't noticed that question –  Mar 23 '16 at 07:42
  • 1
    No worries, @IanBell - Actually I had an idea for an answer but when I Googled to fact-check something it was the SO question that came up first in the results. – Geoff Atkins Mar 23 '16 at 07:43

1 Answers1

1

Try this

<?php
    $externalData = array(array("a","b",3,"d"), array("f","g",1,"h"));

    echo "<pre>";
    print_r($externalData);
    usort($externalData, function ($a, $b) {
         return $a[2] - $b[2];
    });
    echo "<br>";
    print_r($externalData);
    echo "</pre>";
?>

Chech here : https://eval.in/540934

quazardous
  • 846
  • 10
  • 15
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109