php - How do I control the order of this multidimensional array, based on a particular value? -
php - How do I control the order of this multidimensional array, based on a particular value? -
after reading several posts here subject, unable find exact reply problem.
my current array is:
array ( [0] => array ( [level] => bronze [name] => jp morgan [logo] => logo ) [1] => array ( [level] => bronze [name] => nasdaq omx [logo] => logo ) [2] => array ( [level] => platinum [name] => credit suisse [logo] => logo ) [3] => array ( [level] => silver [name] => bnp paribas [logo] => logo ) )
what want have sponsors sorted [level]
of order is:
how do this?
you define own sorting function. example, you'd specify own custom order want follow:
$sort_order = array('platinum', 'gold', 'silver', 'bronze');
pass array info custom sort function, , search value's key position (in case, level
) in sort order you've defined:
$array = array( array ( 'level' => 'bronze', 'name' => 'jp morgan', 'logo' => 'logo' ), array ( 'level' => 'bronze', 'name' => 'nasdaq omx', 'logo' => 'logo' ), array ( 'level' => 'platinum', 'name' => 'credit suisse', 'logo' => 'logo' ), array ( 'level' => 'silver', 'name' => 'bnp paribas', 'logo' => 'logo' ), ); usort($array, function ($a, $b) utilize ($sort_order) { homecoming array_search($a['level'], $sort_order) - array_search($b['level'], $sort_order); }); print_r($array);
outputs:
array ( [0] => array ( [level] => platinum [name] => credit suisse [logo] => logo ) [1] => array ( [level] => silver [name] => bnp paribas [logo] => logo ) [2] => array ( [level] => bronze [name] => nasdaq omx [logo] => logo ) [3] => array ( [level] => bronze [name] => jp morgan [logo] => logo ) )
php arrays multidimensional-array asort
Comments
Post a Comment