php 数组排序问题

WBOY
Lepaskan: 2016-06-06 20:46:09
asal
1168 orang telah melayarinya

<code>$arr = array(
        'id' => 1, 'pid' => 0, 'title' => '1',
        'id' => 2, 'pid' => 0, 'title' => '2',
        'id' => 3, 'pid' => 0, 'title' => '3',
        'id' => 4, 'pid' => 0, 'title' => '4',
        'id' => 5, 'pid' => 1, 'title' => '1-5',
        'id' => 6, 'pid' => 5, 'title' => '1-5-6',
        'id' => 7, 'pid' => 1, 'title' => '1-7',
        'id' => 8, 'pid' => 2, 'title' => '2-8',
    );
</code>
Salin selepas log masuk
Salin selepas log masuk

排序完效果

<code>$arr = array(
        'id' => 1, 'pid' => 0, 'title' => '1',
        'id' => 5, 'pid' => 1, 'title' => '1-5',
        'id' => 6, 'pid' => 5, 'title' => '1-5-6',
        'id' => 7, 'pid' => 1, 'title' => '1-7',
        'id' => 2, 'pid' => 0, 'title' => '2',
        'id' => 8, 'pid' => 2, 'title' => '2-8',
        'id' => 3, 'pid' => 0, 'title' => '3',
        'id' => 4, 'pid' => 0, 'title' => '4',
    );
</code>
Salin selepas log masuk
Salin selepas log masuk

就是父节点后面紧跟子节点

回复内容:

<code>$arr = array(
        'id' => 1, 'pid' => 0, 'title' => '1',
        'id' => 2, 'pid' => 0, 'title' => '2',
        'id' => 3, 'pid' => 0, 'title' => '3',
        'id' => 4, 'pid' => 0, 'title' => '4',
        'id' => 5, 'pid' => 1, 'title' => '1-5',
        'id' => 6, 'pid' => 5, 'title' => '1-5-6',
        'id' => 7, 'pid' => 1, 'title' => '1-7',
        'id' => 8, 'pid' => 2, 'title' => '2-8',
    );
</code>
Salin selepas log masuk
Salin selepas log masuk

排序完效果

<code>$arr = array(
        'id' => 1, 'pid' => 0, 'title' => '1',
        'id' => 5, 'pid' => 1, 'title' => '1-5',
        'id' => 6, 'pid' => 5, 'title' => '1-5-6',
        'id' => 7, 'pid' => 1, 'title' => '1-7',
        'id' => 2, 'pid' => 0, 'title' => '2',
        'id' => 8, 'pid' => 2, 'title' => '2-8',
        'id' => 3, 'pid' => 0, 'title' => '3',
        'id' => 4, 'pid' => 0, 'title' => '4',
    );
</code>
Salin selepas log masuk
Salin selepas log masuk

就是父节点后面紧跟子节点

<code><?php $arr = array(
        array('id' => 1, 'pid' => 0, 'title' => '1'),
        array('id' => 2, 'pid' => 0, 'title' => '2'),
        array('id' => 3, 'pid' => 0, 'title' => '3'),
        array('id' => 4, 'pid' => 0, 'title' => '4'),
        array('id' => 5, 'pid' => 1, 'title' => '1-5'),
        array('id' => 6, 'pid' => 5, 'title' => '1-5-6'),
        array('id' => 7, 'pid' => 1, 'title' => '1-7'),
        array('id' => 8, 'pid' => 2, 'title' => '2-8'),
    );

function userSort($a, $b) {
    $a = $a['title'];
    $b = $b['title'];
    if($a == $b) return 0;
    return $a>$b ? 1 : -1;
}
usort($arr, 'userSort');

echo "

";
print_r($arr);
</code>
Salin selepas log masuk
Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan