How to implement PHP grouping based on the value of an array

不言
Release: 2023-04-01 06:20:02
Original
7555 people have browsed it

This article mainly introduces the method of grouping according to the value of the array in PHP, involving the traversal, judgment, assignment and other related operation skills of the PHP array. Friends in need can refer to the following

The examples of this article are described PHP implements a method of grouping according to the value of an array. Share it with everyone for your reference, the details are as follows:

PHP groups according to the value of the array. This function does not come with PHP array but it is very commonly used. I wrote it today to record it.

Code:

$_array = array(
    array(1,11,'2016-05-18'),
    array(2,11,'2016-05-18'),
    array(3,22,'2016-05-18'),
    array(4,22,'2016-05-18'),
    array(5,33,'2016-05-19'),
    array(6,33,'2016-05-19'),
    array(7,44,'2016-05-19'),
    array(8,44,'2016-05-19'),
    array(9,55,'2016-05-20'),
    array(10,55,'2016-05-20'),
);
var_dump(array_val_chunk($_array));
function array_val_chunk($array){
    $result = array();
    foreach ($array as $key => $value) {
      $result[$value[1].$value[2]][] = $value;
    }
    $ret = array();
    //这里把简直转成了数字的,方便同意处理
    foreach ($result as $key => $value) {
      array_push($ret, $value);
    }
    return $ret;
}
Copy after login

The running results are as follows:

array(5) {
 [0]=>
 array(2) {
  [0]=>
  array(3) {
   [0]=>
   int(1)
   [1]=>
   int(11)
   [2]=>
   string(10) "2016-05-18"
  }
  [1]=>
  array(3) {
   [0]=>
   int(2)
   [1]=>
   int(11)
   [2]=>
   string(10) "2016-05-18"
  }
 }
 [1]=>
 array(2) {
  [0]=>
  array(3) {
   [0]=>
   int(3)
   [1]=>
   int(22)
   [2]=>
   string(10) "2016-05-18"
  }
  [1]=>
  array(3) {
   [0]=>
   int(4)
   [1]=>
   int(22)
   [2]=>
   string(10) "2016-05-18"
  }
 }
 [2]=>
 array(2) {
  [0]=>
  array(3) {
   [0]=>
   int(5)
   [1]=>
   int(33)
   [2]=>
   string(10) "2016-05-19"
  }
  [1]=>
  array(3) {
   [0]=>
   int(6)
   [1]=>
   int(33)
   [2]=>
   string(10) "2016-05-19"
  }
 }
 [3]=>
 array(2) {
  [0]=>
  array(3) {
   [0]=>
   int(7)
   [1]=>
   int(44)
   [2]=>
   string(10) "2016-05-19"
  }
  [1]=>
  array(3) {
   [0]=>
   int(8)
   [1]=>
   int(44)
   [2]=>
   string(10) "2016-05-19"
  }
 }
 [4]=>
 array(2) {
  [0]=>
  array(3) {
   [0]=>
   int(9)
   [1]=>
   int(55)
   [2]=>
   string(10) "2016-05-20"
  }
  [1]=>
  array(3) {
   [0]=>
   int(10)
   [1]=>
   int(55)
   [2]=>
   string(10) "2016-05-20"
  }
 }
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Use PHP to implement Chinese pinyin conversion and Gregorian and lunar calendar conversion

How to solve PHP mkdir() error Write permission issue

The above is the detailed content of How to implement PHP grouping based on the value of an array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!