菜单

How does PHP implement grouping according to the value of an array?

2017年11月22日 - PHPer

In this paper, an example is given to illustrate how PHP implements grouping according to the value of an array. To share with you for your reference, as follows:

PHP group according to the value of the array, PHP array does not own this function, but it is very common, written today to record it.

Code:

$_array=array

Array (1,11, -05-18′),

Array (2,11, -05-18′),

Array (3,22, -05-18′),

Array (4,22, -05-18′),

Array (5,33, -05-19′),

Array (6,33, -05-19′),

Array (7,44, -05-19′),

Array (8,44, -05-19′),

Array (9,55, -05-20′),

Array (10,55, -05-20′),

);

Var_dump (array_val_chunk ($_array));

Functionarray_val_chunk ($array) {

($result=array);

Foreach ($arrayas$key=> $value) {

$result[$value[1].$value[2]][] =$value;

}

($ret=array);

/ / here to turn into digital simply, convenient to handle

Foreach ($resultas$key=> $value) {

Array_push ($ret, $value);

}

Return$ret;

}

The running results are as follows:

Array (5) {

[0]=>

{array (2)

[0]=>

(3) {array

[0]=>

Int (1)

[1]=>

Int (11)

[2]=>

String (10) 2016-05-18″

}

[1]=>

(3) {array

[0]=>

Int (2)

[1]=>

Int (11)

[2]=>

String (10) 2016-05-18″

}

}

[1]=>

{array (2)

[0]=>

(3) {array

[0]=>

Int (3)

[1]=>

Int (22)

[2]=>

String (10) 2016-05-18″

}

[1]=>

(3) {array

[0]=>

Int (4)

[1]=>

Int (22)

[2]=>

String (10) 2016-05-18″

}

}

[2]=>

{array (2)

[0]=>

(3) {array

[0]=>

Int (5)

[1]=>

Int (33)

[2]=>

String (10) 2016-05-19″

}

[1]=>

(3) {array

[0]=>

Int (6)

[1]=>

Int (33)

[2]=>

String (10) 2016-05-19″

}

}

[3]=>

{array (2)

[0]=>

(3) {array

[0]=>

Int (7)

[1]=>

Int (44)

[2]=>

String (10) 2016-05-19″

}

[1]=>

(3) {array

[0]=>

Int (8)

[1]=>

Int (44)

[2]=>

String (10) 2016-05-19″

}

}

[4]=>

{array (2)

[0]=>

(3) {array

[0]=>

Int (9)

[1]=>

Int (55)

[2]=>

String (10) 2016-05-20″

}

[1]=>

(3) {array

[0]=>

Int (10)

[1]=>

Int (55)

[2]=>

String (10) 2016-05-20″

}

}

}

发表评论

电子邮件地址不会被公开。