Home  >  Article  >  Backend Development  >  php二维数组排序解决思路

php二维数组排序解决思路

WBOY
WBOYOriginal
2016-06-13 12:11:27780browse

php二维数组排序
$arr = array (
  array ('desc' => 'pic13.jpg'),
  array ('desc' => 'pic18.jpg'),
  array ('desc' => 'pic21.jpg'),
  array ('desc' => 'pic22.jpg'),
  array ('desc' => 'pic23.jpg'),
  array ('desc' => 'pic5.jpg'),
  array ('desc' => 'pic6.jpg'),
  array ('desc' => 'pic7.jpg'),
  array ('desc' => 'pic8.jpg'),
  array ('desc' => 'pic9.jpg'),
);

数组的desc按照顺序排列,值按照从小到大。
------解决思路----------------------

$arr = array (<br />  array ('desc' => 'pic13.jpg'),<br />  array ('desc' => 'pic18.jpg'),<br />  array ('desc' => 'pic21.jpg'),<br />  array ('desc' => 'pic22.jpg'),<br />  array ('desc' => 'pic23.jpg'),<br />  array ('desc' => 'pic5.jpg'),<br />  array ('desc' => 'pic6.jpg'),<br />  array ('desc' => 'pic7.jpg'),<br />  array ('desc' => 'pic8.jpg'),<br />  array ('desc' => 'pic9.jpg'),<br />);<br />foreach($arr as $v) $r[] = substr($v['desc'], 3, -4);<br />array_multisort($r, $arr);<br />print_r($arr);
Array
(
    [0] => Array
        (
            [desc] => pic5.jpg
        )

    [1] => Array
        (
            [desc] => pic6.jpg
        )

    [2] => Array
        (
            [desc] => pic7.jpg
        )

    [3] => Array
        (
            [desc] => pic8.jpg
        )

    [4] => Array
        (
            [desc] => pic9.jpg
        )

    [5] => Array
        (
            [desc] => pic13.jpg
        )

    [6] => Array
        (
            [desc] => pic18.jpg
        )

    [7] => Array
        (
            [desc] => pic21.jpg
        )

    [8] => Array
        (
            [desc] => pic22.jpg
        )

    [9] => Array
        (
            [desc] => pic23.jpg
        )

)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn