This article mainly introduces how PHP sorts two-dimensional arrays by specified key values. it involves traversing PHP two-dimensional arrays and using array_multisort functions, for more information about how PHP sorts two-dimensional arrays by specified key values, see the following example. We will share this with you for your reference. The details are as follows:
Problem:
Array:
The code is as follows:
Array (0 => array ('id' => 1, 'price' => 50), 1 => array ('id' => 2, 'price' => 60 ));
Sort by the price field of the array.
The implementation code is as follows:
<?php $array[] = array('id'=>1,'price'=>50);$array[] = array('id'=>2,'price'=>70);$array[] = array('id'=>3,'price'=>30);$array[] = array('id'=>4,'price'=>20);foreach ($array as $key=>$value){ $id[$key] = $value['id']; $price[$key] = $value['price'];}array_multisort($price,SORT_NUMERIC,SORT_DESC,$id,SORT_STRING,SORT_ASC,$array);echo '';print_r($array);echo '
';?>
Running result:
Array([0] => Array([id] => 2[price] => 70)[1] => Array([id] => 1[price] => 50)[2] => Array([id] => 3[price] => 30)[3] => Array([id] => 4[price] => 20))
I hope this article will help you with PHP programming.