PHP search multidimensional array for value, then place that element at start of array

Posted by BobFlemming on Stack Overflow See other posts from Stack Overflow or by BobFlemming
Published on 2012-04-05T11:06:37Z Indexed on 2012/04/05 11:28 UTC
Read the original article Hit count: 241

Filed under:
|
|

I need to search this array:

cars - 
      [0] -make : Ford
          -model: Escort
          -year: 1991

      [1] -make: Honda
          -model: Civic
          -year: 1996

      [2] -make: Vauxhall 
          -model: Astra
          -year: 1972

And if (for example) the model is "Civic" , place that 'car' at position 0.

So the end array would be like:

cars - 
      [0] -make: Honda
          -model: Civic
          -year: 1996

      [1] -make : Ford
          -model: Escort
          -year: 1991        

      [2] -make: Vauxhall 
          -model: Astra
          -year: 1972

I've tried some usort variations:

  function typeSort($a, $b)
    {
        if ($a['model'] == 'Civic' )
        {
            return 0;
        }
        return ($a['model'] < $b['model']) ? -1 : 1;
    }

but this is just returning 1

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays