PHP counts the number of elements of a one-dimensional array value identical to num, and converts it to subscript as a number, and the value is a two-dimensional array of both value and num

Source: Internet
Author: User
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);}
  • 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.