PHP to sort a two-dimensional array by specifying key-value keys _php tips for Sample code

Source: Internet
Author: User
Tags array sort

Copy Code code as follows:

function Array_sort ($array, $key) {
if (Is_array ($array)) {
$key _array = null;
$new _array = null;
for ($i = 0; $i < count ($array); $i + +) {
$key _array[$array [$i] [$key]] = $i;
}
Ksort ($key _array);
$j = 0;
foreach ($key _array as $k => $v) {
$new _array[$j] = $array [$v];
$j + +;
}
unset ($key _array);
return $new _array;
}else{
return $array;
}
}

PHP two-dimensional array key value ordering

PHP Array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional. The association key name remains unchanged, but the numeric key name is indexed. The input array is treated as a table column and sorted by row, and the first array is the primary array to sort. The rows (values) in the array are the same, sorted by the size of the corresponding values in the next input array, and so on.

But if the array you want to sort is a two-dimensional array that needs to be sorted by the key values of the array, such as the following two-dimensional array, which needs to be sorted by the sort key name, then Array_multisort () cannot be directly implemented:

$data [5] = Array (' volume ' =>, ' Edition ' => 2);
$data [4] = Array (' volume ' =>, ' edition ' => 1);
$data [2] = Array (' volume ' =>, ' Edition ' => 6);
$data [3] = Array (' volume ' =>, ' Edition ' => 2);
$data [1] = Array (' volume ' =>, ' Edition ' => 6);
$data [6] = Array (' volume ' =>, ' edition ' => 7);
Prepares the array to sort by
foreach ($data as $k => $v) {
  $edition [] = $v [' edition '];
}
Array_multisort ($edition, SORT_ASC, $data);
Print_r ($data);

Will output:

Array
(
  [0] => array
    (
      [volume] =>
      [Edition] => 1
    )

  [1] => array
    (
      [Volume] =>
      [Edition] => 2
    )

  [2] => Array
    (
      [volume] =>
      [Edition] => 2
    )

  [3] => array
    (
      [volume] =>
      [Edition] => 6
    )

  [4] => array
    ( C24/>[volume] =>
      [Edition] => 6

  [5] => Array
    (
      [volume] =>
      [Edition] => 7
    )

)

To not break the original key, a sort function is written to support only two-dimensional arrays.

/** * sorted according to the size of a key value in the array, only two-dimensional array * * @param array $array ordered array * @param string $key key value * @p Aram BOOL $ASC Default positive sequence * @return array sort after arrays/function Arraysortbykey (array $array, $key, $asc = True) {$result = array
  ();
  Sort out the array to be sorted foreach ($array as $k => & $v) {$values [$k] = Isset ($v [$key])? $v [$key]: ";
  } unset ($v); Do you want to sort $asc for sort key values?
  Asort ($values): Arsort ($values);
  Rearrange the original array foreach ($values as $k => $v) {$result [$k] = $array [$k];
return $result; }
Related Article

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.