PHP counts the number of elements of a one-dimensional array value equal to num, and converts it to a two-dimensional array of subscript numbers, value and num
A recent project that queries a field from a database to get an array key is a one-dimensional array of channel $res, and now needs to turn this array into a key that is the number value is channel and num (num is the same channel number, the default is 0).
$res Array
Array (size=2) 0 = string ' 0 ' (length=1) 1 = = String ' 500000009 ' (length=9) 2 = String ' 500000009 ' (length=9)
$rs Array
Array (size=2) 0 = = int 1 500000009 = int 2
$arr Array
Array (size=2) 0 = = Array (size=2) ' channel ' = + int 0 ' num ' + int 1 1 = = Array (size =2) ' channel ' = = int 500000009 ' num ' = int 2
Working with Code
foreach ($res as $v) {//Get a key is a one-dimensional array of channel values as numbers $rs [$v]++;} $arr = Array (); foreach ($rs as $key = = $value) {//Convert an array of Rs to a key is a number, the value is a two-dimensional array of channel and Num $arr [] = Array (' channel ' = > $key, ' num ' = $value);}