How do I get the value of a specified key from multiple keys in a two-dimensional array?

Source: Internet
Author: User
Tags call back

EssenceLOVEME962016-10-21 10:40:19 browse (1512) Answer (3) like (0)

Novice to ask: two-dimensional array will generally have more than one key, if we want to get the value of the specified key, what should be done?

Question label:PHP Answer (3)Timberswift 2016-10-21

The first: The simplest method: The foreach traversal array, the code:

foreach ($arr as $key => $value) {$arr2[] = $value[‘name‘];}

Another method: Using the Array_map $arr 2 = array_map (' Array_shift ', $arr); Moves the value of the beginning of each value of the $arr array out and returns the value that is moved out of each item value, noting that the key of the new array $arr2 is still the key of the original array $arr.

You can also do this if you need to get the beginning or end column of each item of a two-dimensional array: $arr 2 = array_map (' End ', $arr); Haha, is also very convenient.

0 0 share 0 report Timberswift 2016-10-21

The third method: The Array_reduce method, but the code is slightly more, but this method of the imaginary space (for other array value operation) is still quite large:

$arr2 = 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;

0 0 share 0 report Timberswift 2016-10-21

Fourth: This method is also very simple, and the most flexible

$arr2 = 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 >= 5.5.0, use this method in a stale project or you have to think about it. Gets a value in a two-dimensional array randomly.

$arr 1 =Array (<span style="White-space:pre" > </span>' P1 ' =Array' Ky_d ' =' Quanzhou Fengze District, Fujian Province, China 1 ', ky_s ' => ' 350503005 '),  <span style=" White-space:pre "> </span> ' P2 ' => Array ( ' ky_d ' => ' Fujian Quanzhou Fengze District hua da Jie 2 ',  ky_s ' => ' 350503006 '),  <span style=" White-space:pre "> </span> ' P3 ' => Array ( ' ky_d ' => ' Fujian Quanzhou Fengze District hua da Jie 3 ',  ky_s ' => ' 350503007 ')); $temp = Array_rand ($arr 1,1);           

Gets all values for a particular key (array subscript) of a one-dimensional or multidimensional array.

author:[email protected]sina.cn description:

Take out all the values of a one-dimensional or multidimensional array based on a particular key (subscript), instead of looping, consider the efficiency of a large array, serialize the array, and extract the required strings based on the characteristics of the serialization structure.

functionArray_get_by_key(array $array, $string) {<span style="White-space:pre" > </span>if (!trim ($string))ReturnFalse;<span style="White-space:pre" > </span>preg_match_all ("/\" $string \ "; \w{1}:(?: \ d+:|) (. *);/", Serialize ($array), $res); <span style="White-space:pre" > </span>return $res [1];} $r =array ( ' id ' = 1,  ' s ' =" 23,  ' a ' = array ( ' s ' = 123, array (1, 2,  ' s ' = =  "ASDASDGSADGGSADG")); echo  ' <pre> ';p rint_r (Array_get_by_key ($r, /*result:array (<span style= "White-space:pre" > </span>[0] = 23<span style= "White-space:pre" > </span>[1] + 123<span style= "White-space:pre" > </span>[2] = " ASDASDGSADGGSADG ") */             

How can I get the value of a specified key from multiple keys in a two-dimensional array?

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.