PHP gets a collection of values for a column in a two-dimensional array, _php tutorial

Source: Internet
Author: User
Tags call back php sample code

PHP gets a collection of values for a column in a two-dimensional array,


PHP is still more commonly used, so I studied php two-dimensional array. In the process of working with PHP arrays, there is a particular need for frequent, two-dimensional arrays such as:

$arr = Array (1=>array (' id ' = = 5, ' name ' = ' Zhang San '), 2=>array (' id ' = = 6, ' name ' = ' John Doe '));

The goal is to take the key to the name collection and get this result:

$arr 2 = Array (0=> ' Zhang San ',1=> ' John Doe ');

Here are a few ways:

1: The simplest, foreach traversal array:

foreach ($arr as $key = + $value) {$arr 2[] = $value [' name '];}

2: Code a bit less code, using Array_map this PHP method:

$arr 2 = array_map (' Array_shift ', $arr);

Remove the value from the beginning of each value of the $arr array and return the value that was moved out of each item value, noting that the key of the new array $arr2 is still the key of the original array $arr

2.1: On the basis of Method 2, you can open a little bit of the brain hole, if you need to get a two-dimensional array of each item of the beginning column or the end column, you can also do this:

$arr 2 = array_map (' Reset ', $arr); $arr 2 = Array_map (' End ', $arr);

Haha, is also very convenient

3: You can also use the Array_reduc E method, but the code is slightly more, but this method of the imaginary space (for other array value operation) is still quite large:

$arr 2 = array_reduce ($arr, Create_function (' $result, $v ', ' $result [] = $v ["name"];return $result; ')];

The Array_reduce method uses the callback function to iterate over the value of the array, and create_function is used for an anonymous method to call back, the parameter of the anonymous method $result the value generated for the last iteration, $v is the current value, and the internal implementation is both getting to the array $ The value of "name" for each entry of ARR and push to the new $result array;

4: Finally the ultimate method is really cool, a method is done, and very flexible:

$arr 2 = array_column ($arr, ' name ');

The second parameter is the key name of the column you want to get, is not very convenient, but this method has a limitation, that is, PHP version must be >= 5.5.0, use this method in a stale project you have to think about it.

Several ways of traversing two-dimensional arrays in ps:php

<?php//uses the For loop to traverse $arr2=array (Array ("Zhang San", "20", "male"), Array ("John Doe", "25", "male"), Array ("Harry", "19", "female"), Array ("Zhao Liu", "25", "female"); echo "
"For ($i =0; $i <4; $i + +) {echo" "For ($j =0; $j <3; $j + +) {echo" ";} echo " "; echo" ";} echo "
Name Age Sex
"; echo $arr 2[$i [$j]; echo "
";? >//uses a foreach traversal <?php$arr = Array (' One ' =>array (' name ' = ' = ' Zhang San ', ' age ' = ' ' ", ' sex ' = ' male '), ' one '" = Array (' name ' = ' = ' John Doe ', ' age ' = ' + ', ' sex ' = ' female '), ' three ' =>array (' name ' = ' = ' Harry ', ' age ' = ' + ', ' sex ' = > ' Male '), ' Four ' =>array (' name ' = ' Zhao Liu ', ' age ' = ' + ', ' sex ' = ' female '); foreach ($arr as $k + $val) {echo $val [' Name ']. $val [' age ']. $val [' sex ']. "
";} echo "

";? ><?php$arr = Array (' One ' =>array (' name ' = ' = ' Zhang San ', ' age ' = ' ~ ', ' sex ' = ' male '), ' One ' =>array (' name ' = > ' John Doe ', ' age ' = ' + ', ' sex ' = ' female ', ' three ' =>array (' name ' = ' Harry ', ' age ' = ' + ', ' sex ' = ' male '), ' four ' = >array (' name ' = ' Zhao Liu ', ' age ' = ' + ', ' sex ' = ' female '); foreach ($arr as $key = + $value) {foreach ($value as $key 2 = > $value 2) {echo $value 2;} echo "
";}? >

Articles you may be interested in:

    • PHP sample code for two-dimensional arrays sorted by the specified key value key
    • A function sharing of a PHP two-dimensional array ordering
    • Example of a PHP two-dimensional array being converted to a string
    • PHP two-dimensional array sorting 3 ways and custom function sharing
    • How PHP gets the set of a key in a two-dimensional array
    • thinkphp a two-dimensional array into a label applicable one-dimensional array method summary

http://www.bkjia.com/PHPjc/1084521.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084521.html techarticle PHP gets a set of values for a column in a two-dimensional array, PHP is more commonly used, so I studied php two-dimensional arrays. When working with PHP arrays, there is a special need, such as ...

  • 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.