<?php
$arr = '';
echo '二维数组如下:'.'<br / >';
for($i=0; $i<=5; $i++)
{
$arr[$i]['val'] = mt_rand(1, 100);
$arr[$i]['num'] = mt_rand(1, 100);
}
echo '<pre>';
print_r($arr);
echo '</pre>';
echo '从二维数组中抽出键为val,单独成另一个数组:'.'<br / >';
foreach ($arr as $key => $row)
{
$vals[$key] = $row['val'];
$nums[$key] = $row['num'];
}
echo '<pre>';
print_r($vals);
echo '</pre>';
echo '对其进行排序:'.'<br / >';
array_multisort($vals, SORT_ASC, $arr);
echo '<pre>';
print_r($vals);
echo '</pre>';
?>
运行结果:
二维数组如下:
Array
(
[0] => Array
(
[val] => 46
[num] => 49
)
[1] => Array
(
[val] => 8
[num] => 24
)
[2] => Array
(
[val] => 37
[num] => 3
)
[3] => Array
(
[val] => 32
[num] => 35
)
[4] => Array
(
[val] => 19
[num] => 38
)
[5] => Array
(
[val] => 30
[num] => 37
)
)
从二维数组中抽出键为val,单独成另一个数组:
Array
(
[0] => 46
[1] => 8
[2] => 37
[3] => 32
[4] => 19
[5] => 30
)
对其进行排序:
Array
(
[0] => 8
[1] => 19
[2] => 30
[3] => 32
[4] => 37
[5] => 46
)
我们将得到一个按val升序排序的二维数组。
您可能感兴趣的文章: