PHP sort multidimensional Array by specific key value

Try usort for multidimensional array sorting by specific key value inside each sub array.
Example:


array (size=3)
  0 => 
    array (size=3)
      'id' => string '137880625719tzdmr' (length=17)
      'name' => string 'Retailer1' (length=9)
      'value' => string 'A' (length=1)
  1 => 
    array (size=3)
      'id' => string '137886379776hyups' (length=17)
      'name' => string 'Retailer YZ' (length=11)
      'value' => string 'b' (length=1)
  2 => 
    array (size=3)
      'id' => string '137888317905yjrxk' (length=17)
      'name' => string 'Retailer Spec' (length=13)
      'value' => string 'c' (length=1)

The best way is:

	usort(<your array>, function ($elem1, $elem2) {
        		return strcmp( lcfirst($elem1['name']), lcfirst($elem2['name']) );
        	});

reference:

http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2

By Keenlio, July 14, 2014

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *


seven × = 49

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>