PHP using the foreach Transform array steps

Source: Internet
Author: User
This time for you to bring PHP to use the foreach Transformation array step-by-bit, PHP using a foreach conversion array of considerations, the following is the actual case, together to see.

Requirements:

The two-dimensional array $arr is converted to a two-dimensional array with ' time ' and ' type ' as the subscript and ' data ' values;

Original array:

$arr = Array (   0 = = Array (    ' data ' = =,    ' type ' = = 1, ' time ' = '    2018-01-26 ',   ),   1 =& Gt Array (    ' data ' = $,    ' type ' = = 2,    ' time ' = ' 2018-01-26 ',   ),   2 = = Array (    ' Data ' + ',    ' type ' = 2,    ' time ' = ' 2018-01-27 ', ' 3 ' =   Array (    ' data ' = 400,    ' type ' = 3,    ' time ' = ' 2018-01-27 ', '   4 = = Array ' ('    data ' = ' = ', '    type ' =& Gt 4,    ' time ' = ' 2018-01-28 ',   )  ;

Transformation:

foreach ($arr as $key = + $value) {$change [$value [' Time ']][$value [' type ']] = $value [' Data '];}

Results:

Array (3) {["2018-01-26"] = = Array (2) {[1] = = Int (+) [2] = Int (+)} ["2018-01-27"] = + Array (2) {[2] =&G T int (+) [3] = = Int (+)} ["2018-01-28"] = = Array (1) {[4] = Int (500)}}

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP implementation of regular expression grouping capture steps in detail

PHP Mb_strpos use steps in detail

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.