php 数组排序问题

WBOY
Libérer: 2016-06-06 20:46:09
original
1123 Les gens l'ont consulté

<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>
Copier après la connexion
Copier après la connexion

排序完效果

<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>
Copier après la connexion
Copier après la connexion

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

回复内容:

<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>
Copier après la connexion
Copier après la connexion

排序完效果

<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>
Copier après la connexion
Copier après la connexion

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

<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>
Copier après la connexion
Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!