Maison  >  Article  >  développement back-end  >  Tri de tableau bidimensionnel PHP array_multisort

Tri de tableau bidimensionnel PHP array_multisort

不言
不言original
2018-05-15 10:36:142959parcourir

Le contenu principal de cet article concerne le tri des tableaux bidimensionnels PHP array_multisort. Il a une certaine valeur de référence. Maintenant, je le partage avec vous. Les amis dans le besoin peuvent s'y référer

Pour les tableaux à 2 dimensions. ou tableaux multidimensionnels Le tri est un problème courant. En PHP, nous avons une fonction spéciale de tri des tableaux multidimensionnels. Voici une brève introduction :

array_multisort(array1,sorting order, sorting type,array2,array3..) 是对多个数组或多维数组进行排序的函数。
array1 必需。规定输入的数组。
sorting order 可选。规定排列顺序。可能的值是 SORT_ASC 和 SORT_DESC。
sorting type 可选。规定排序类型。可能的值是SORT_REGULAR、SORT_NUMERIC和SORT_STRING。
array2 可选。规定输入的数组。
array3 可选。规定输入的数组。

Le tableau dans le. Le paramètre est traité comme une colonne de table et fusionné. Trier par ligne - Ceci est similaire à la fonctionnalité de la clause ORDER BY de SQL. Le premier tableau est le tableau principal à trier. Si les lignes (valeurs) du tableau sont identiques, elles seront triées en fonction de la taille de la valeur correspondante dans le tableau d'entrée suivant, et ainsi de suite.
Le premier paramètre est un tableau, et chaque paramètre suivant peut être un tableau ou l'un des indicateurs d'ordre de tri suivants (les indicateurs de tri sont utilisés pour modifier l'ordre de tri par défaut) :
SORT_ASC - arrangement par défaut, par ordre croissant . (A-Z)
SORT_DESC - Trier par ordre décroissant. (Z-A)
Le type de tri peut alors être précisé :
SORT_REGULAR - par défaut. Disposez chaque élément dans un ordre régulier.
SORT_NUMERIC - Triez chaque élément par ordre numérique.

SORT_STRING - Organisez chaque élément par ordre alphabétique

<?php 
   
    function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){  
        if(is_array($arrays)){  
            foreach ($arrays as $array){  
                if(is_array($array)){  
                    $key_arrays[] = $array[$sort_key];  
                }else{  
                    return false;  
                }  
            }  
        }else{  
            return false;  
        } 
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);  
        return $arrays;  
    } 
   
    $person =  array( 
                    array(&#39;id&#39;=>1,&#39;name&#39;=>&#39;fj&#39;,&#39;weight&#39;=>100,&#39;height&#39;=>180), 
                    array(&#39;id&#39;=>2,&#39;name&#39;=>&#39;tom&#39;,&#39;weight&#39;=>53,&#39;height&#39;=>150), 
                    array(&#39;id&#39;=>3,&#39;name&#39;=>&#39;jerry&#39;,&#39;weight&#39;=>120,&#39;height&#39;=>156), 
                    array(&#39;id&#39;=>4,&#39;name&#39;=>&#39;bill&#39;,&#39;weight&#39;=>110,&#39;height&#39;=>190), 
                    array(&#39;id&#39;=>5,&#39;name&#39;=>&#39;linken&#39;,&#39;weight&#39;=>80,&#39;height&#39;=>200), 
                    array(&#39;id&#39;=>6,&#39;name&#39;=>&#39;madana&#39;,&#39;weight&#39;=>95,&#39;height&#39;=>110), 
                    array(&#39;id&#39;=>7,&#39;name&#39;=>&#39;jordan&#39;,&#39;weight&#39;=>70,&#39;height&#39;=>170) 
                ); 
       
    var_dump($person); 
       
    $person = my_sort($person,&#39;name&#39;,SORT_ASC,SORT_STRING); 
   
    var_dump($person); 
       
    $person = my_sort($person,&#39;weight&#39;); 
   
    var_dump($person); 
?>

Le résultat est le suivant :

array (size=7)
  0 =>
    array (size=4)
      &#39;id&#39; => int 1
      &#39;name&#39; => string &#39;fj&#39; (length=2)
      &#39;weight&#39; => int 100
      &#39;height&#39; => int 180
  1 =>
    array (size=4)
      &#39;id&#39; => int 2
      &#39;name&#39; => string &#39;tom&#39; (length=3)
      &#39;weight&#39; => int 53
      &#39;height&#39; => int 150
  2 =>
    array (size=4)
      &#39;id&#39; => int 3
      &#39;name&#39; => string &#39;jerry&#39; (length=5)
      &#39;weight&#39; => int 120
      &#39;height&#39; => int 156
  3 =>
    array (size=4)
      &#39;id&#39; => int 4
      &#39;name&#39; => string &#39;bill&#39; (length=4)
      &#39;weight&#39; => int 110
      &#39;height&#39; => int 190
  4 =>
    array (size=4)
      &#39;id&#39; => int 5
      &#39;name&#39; => string &#39;linken&#39; (length=6)
      &#39;weight&#39; => int 80
      &#39;height&#39; => int 200
  5 =>
    array (size=4)
      &#39;id&#39; => int 6
      &#39;name&#39; => string &#39;madana&#39; (length=6)
      &#39;weight&#39; => int 95
      &#39;height&#39; => int 110
  6 =>
    array (size=4)
      &#39;id&#39; => int 7
      &#39;name&#39; => string &#39;jordan&#39; (length=6)
      &#39;weight&#39; => int 70
      &#39;height&#39; => int 170
array (size=7)
  0 =>
    array (size=4)
      &#39;id&#39; => int 4
      &#39;name&#39; => string &#39;bill&#39; (length=4)
      &#39;weight&#39; => int 110
      &#39;height&#39; => int 190
  1 =>
    array (size=4)
      &#39;id&#39; => int 1
      &#39;name&#39; => string &#39;fj&#39; (length=2)
      &#39;weight&#39; => int 100
      &#39;height&#39; => int 180
  2 =>
    array (size=4)
      &#39;id&#39; => int 3
      &#39;name&#39; => string &#39;jerry&#39; (length=5)
      &#39;weight&#39; => int 120
      &#39;height&#39; => int 156
  3 =>
    array (size=4)
      &#39;id&#39; => int 7
      &#39;name&#39; => string &#39;jordan&#39; (length=6)
      &#39;weight&#39; => int 70
      &#39;height&#39; => int 170
  4 =>
    array (size=4)
      &#39;id&#39; => int 5
      &#39;name&#39; => string &#39;linken&#39; (length=6)
      &#39;weight&#39; => int 80
      &#39;height&#39; => int 200
  5 =>
    array (size=4)
      &#39;id&#39; => int 6
      &#39;name&#39; => string &#39;madana&#39; (length=6)
      &#39;weight&#39; => int 95
      &#39;height&#39; => int 110
  6 =>
    array (size=4)
      &#39;id&#39; => int 2
      &#39;name&#39; => string &#39;tom&#39; (length=3)
      &#39;weight&#39; => int 53
      &#39;height&#39; => int 150
array (size=7)
  0 =>
    array (size=4)
      &#39;id&#39; => int 2
      &#39;name&#39; => string &#39;tom&#39; (length=3)
      &#39;weight&#39; => int 53
      &#39;height&#39; => int 150
  1 =>
    array (size=4)
      &#39;id&#39; => int 7
      &#39;name&#39; => string &#39;jordan&#39; (length=6)
      &#39;weight&#39; => int 70
      &#39;height&#39; => int 170
  2 =>
    array (size=4)
      &#39;id&#39; => int 5
      &#39;name&#39; => string &#39;linken&#39; (length=6)
      &#39;weight&#39; => int 80
      &#39;height&#39; => int 200
  3 =>
    array (size=4)
      &#39;id&#39; => int 6
      &#39;name&#39; => string &#39;madana&#39; (length=6)
      &#39;weight&#39; => int 95
      &#39;height&#39; => int 110
  4 =>
    array (size=4)
      &#39;id&#39; => int 1
      &#39;name&#39; => string &#39;fj&#39; (length=2)
      &#39;weight&#39; => int 100
      &#39;height&#39; => int 180
  5 =>
    array (size=4)
      &#39;id&#39; => int 4
      &#39;name&#39; => string &#39;bill&#39; (length=4)
      &#39;weight&#39; => int 110
      &#39;height&#39; => int 190
  6 =>
    array (size=4)
      &#39;id&#39; => int 3
      &#39;name&#39; => string &#39;jerry&#39; (length=5)
      &#39;weight&#39; => int 120
      &#39;height&#39; => int 156

Le point clé ici est d'abord de sauvegarder la clé à trié dans Dans un tableau unidimensionnel, vous pouvez ensuite utiliser la fonction array_multisort() pour trier le tableau par clé. Bien entendu, vous n'avez pas besoin d'appliquer la fonction array_multisort() au tri ici. uniquement via la traversée foreach. Mais maintenant que les développeurs PHP nous ont fourni un meilleur moyen, nous pouvons nous épargner des problèmes inutiles.

Republié depuis :https://www.cnblogs.com/tdalcn/p/6420055.html

Recommandations associées :

PHP Couper un tableau à deux dimensions en chaînes et supprimer les valeurs en double

Comment trier un champ dans un tableau à une dimension dans un tableau à deux dimensions en php

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
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