'879798'),[1] = >array(  'name' => "a9",  'time' => '85665565'),"/> '879798'),[1] = >array(  'name' => "a9",  'time' => '85665565'),">
Home > Backend Development > PHP Tutorial > 排序有什么更好的步骤

排序有什么更好的步骤

WBOY
Release: 2016-06-13 13:21:37
Original
838 people have browsed it

排序有什么更好的方法
array(
[0] =>array(
 'name' => "a",
 'time' => '879798'),
[1] = >array(
 'name' => "a9",
 'time' => '85665565'),
[2] = >array(
 'name' => "hdhdjd",
 'time' => '8154465'),
[3] = >array(
 'name' => "a",
 'time' => '800000'),
[4] = >array(
 'name' => "dr",
 'time' => '789898'),
);


是一个已经按照time排序好的数组
现在要排这样

array(
[0] =>array(
'name' => "a",
'time' => '879798'),
[1] = >array(
'name' => "a",
'time' => '800000'),
[2] = >array(
 'name' => "a9",
 'time' => '85665565'),
[3] = >array(
 'name' => "hdhdjd",
 'time' => '8154465'),

[4] = >array(
 'name' => "dr",
 'time' => '789898'),
);

把name相同的在一起。以第一个出现的次序为大。

不知道有什么好方式啊,代码写的漂亮点的。

------解决方案--------------------

PHP code
$ar = array(
0 =>array(
 'name' => "a",
 'time' => '879798'),
1 => array(
 'name' => "a9",
 'time' => '856565'), //85665565 ? 示例数据有问题吧,看不出是按time排序的!
2 =>array(
 'name' => "hdhdjd",
 'time' => '815465'),
3 =>array(
 'name' => "a",
 'time' => '800000'),
4 =>array(
 'name' => "dr",
 'time' => '789898'),
);

$result = array();
$len = count($ar);
for ($i = 0; $i ';
print_r($result);

/*
Array
(
    [0] => Array
        (
            [name] => a
            [time] => 879798
        )

    [1] => Array
        (
            [0] => Array
                (
                    [name] => a
                    [time] => 800000
                )

        )

    [2] => Array
        (
            [name] => a9
            [time] => 856565
        )

    [3] => Array
        (
            [name] => hdhdjd
            [time] => 815465
        )

    [4] => Array
        (
            [name] => dr
            [time] => 789898
        )

    [5] => 
)
*/
<br><font color="#e78608">------解决方案--------------------</font><br>有点问题,修改一下:<br>
Copy after login
PHP code
$result = array();
$len = count($ar);
for ($i = 0; $i <font color="#e78608">------解决方案--------------------</font><br>
Copy after login
C/C++ code
<?php $arr = array(
                array(
                         'name' => "a",
                         'time' => '879798'),
                array(
                         'name' => "a9",
                          'time' => '85665565'),
                array(
                         'name' => "dr",
                          'time' => '8154465'),
                array(
                         'name' => "a",
                          'time' => '800000'),
                array(
                         'name' => "dr",
                          'time' => '789898'),
);

$names = array();
$index = array();

array_walk($arr, function($val, $key){
                        global $names;
                        global $index;
                        $names[] = $val['name'];
                        $index[] = $key;
                });

array_multisort($names, $index, $arr);
print_r($arr);
?>
 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template