PHP Simple implementation of multidimensional array sorting method _php tips

Source: Internet
Author: User

This article illustrates the simple way to implement multidimensional array sorting in PHP. Share to everyone for your reference, specific as follows:

Before doing a function, you have to put the data into a two-dimensional array and sort, and then the Internet to find solutions to the idea,

The Array_multisort function is used, and the Array_multisort () function sorts multiple arrays or multidimensional arrays

Let's take a look at an example

<?php
$data =array (
0=>array (' One ' =>34, ' two ' => ' d '),
1=>array (' One ' =>45, ' two ' => ' E '),
2=>array (' One ' =>47, ' two ' => ' H '),
3=>array (' One ' =>12, ' two ' => ' C '),
4=> Array (' One ' =>15, ' two ' => ' W '),
5=>array (' One ' =>85, ' two ' => ' R '),
);
foreach ($data as $val) {
$key _arrays[]= $val [' one '];
}
Array_multisort ($key _arrays,sort_asc,sort_numeric, $data);
Var_dump ($data);

Output Result: Key value one sort, as follows:

Array
 0 => 
  array
   ' one ' => int
   ' two ' => string ' C ' (length=1)
 1 => 
  array
   ' One ' => int
   ' two ' => string ' W ' (length=1)
 2 => 
  array ' one
   ' => int
   ' two ' => St Ring ' d ' (length=1)
 3 => 
  array
   ' one ' => int
   ' two ' => string ' e ' (length=1)
 4 =>
   array
   ' one ' => int
   ' two ' => string ' h ' (length=1)
 5 => 
  array ' one
   ' => int 85< c24/> ' two ' => string ' R ' (length=1)

PHP Multidimensional Array sorting

The following are encapsulated into functions for easy use

function My_array_multisort ($data, $sort _order_field, $sort _order=sort_asc, $sort _type=sort_numeric) {
foreach ($data as $val) {
$key _arrays[]= $val [$sort _order_field];
}
Array_multisort ($key _arrays,sort_asc,sort_numeric, $data);
Rturn $data;
}

For more information about PHP interested readers can view the site topics: "PHP array Operation skills Encyclopedia", "PHP Sorting algorithm Summary", "PHP Basic Grammar Primer", "PHP Operations and Operator Usage Summary", "PHP object-oriented Programming Program Introduction", " PHP Network Programming skills Summary, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.