Example of php getting the key value in a two-dimensional array-PHP source code

Source: Internet
Author: User
There are usually multiple keys in a two-dimensional array. If we want to obtain the value of a specified key, we can take a look at an example of php getting the key value in a two-dimensional array, I hope the article will help you. There are usually multiple keys in a two-dimensional array. If we want to obtain the value of a specified key, we can take a look at an example of php getting the key value in a two-dimensional array, I hope the article will help you.

Script ec (2); script


When processing php arrays, the following two-dimensional arrays are frequently required:

$ Arr = array (
1 => array (
'Id' => 5,
'Name' => 'zhang san'
),
2 => array (
'Id' => 6,
'Name' => 'Li si'
)
);

The purpose is to obtain the set with key as name and get the result:


$ Arr2 = array (

0 => 'zhang san ',

1 => 'Li si'

);
There are several methods:

1: The simplest, foreach traversing the array:

Foreach ($ arr as $ key => $ value ){

$ Arr2 [] = $ value ['name'];

}
2: The php method of array_map is used for a slightly less code:

$ Arr2 = array_map ('array _ shift ', $ arr );

Remove the value starting with each item of the $ arr array and return the value to be removed from each item, note 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 slightly open the brain hole. If you need to obtain the starting or ending columns of each item in a two-dimensional array, you can also do this:



$ Arr2 = array_map ('end', $ arr );

Haha, it's very convenient.

3: The array_reduce method can also be used, but the code is slightly larger. However, the imagination of this method (for other array value operations) is 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. create_function is used for an anonymous method to call back. The parameter $ result of this anonymous method is the value generated in the previous iteration, $ v is the current value. The internal implementation is to get the "name" value of each $ arr item and push it to the new $ result array;

4: In the end, this ultimate method is really cool. It can be done with one method, and it is very flexible:


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

The second parameter is the key name of the column to be obtained. Is it very convenient? However, this method has a limit that the php version must be> = 5.5.0, to use this method in outdated projects, consider it.

Random acquisition of a value in a two-dimensional array

$ Arr1 = array (
'P1' => array ('ky _ d' => '1', 'ky _ s' => '123', Huada Street, fengze district, Quanzhou City, Fujian province '),
'P2' => array ('ky _ d' => 'huada Street, fengze district, Quanzhou City, Fujian Province 2', 'ky _ s' => '123 '),
'P3 '=> array ('ky _ d' => '3', 'ky _ s' => '123', Huada Street, fengze district, Quanzhou City, Fujian Province ')

);
$ Temp = array_rand ($ arr1, 1 );
// Print_r ($ temp );
Echo $ arr1 [$ temp] ['ky _ d']. ':'. $ arr1 [$ temp] ['ky _ s'];

Obtains all values of a specific key (array subscript) in a one-dimensional or multi-dimensional array.

/*
Author: yangyu@sina.cn
Description: Retrieves all values of a one-dimensional or multi-dimensional array based on a specific key (subscript). The reason for not repeating is to serialize arrays based on the efficiency of large arrays, then extract the required string based on the characteristics of the serialization structure.
*/
Function array_get_by_key (array $ array, $ string ){
If (! Trim ($ string) return false;
Preg_match_all ("/\" $ string \ "; \ w {1 }:(? : \ D +: | )(.*?); /", Serialize ($ array), $ res );
Return $ res [1];
}


$ R = array ('id' => 1,'s '=> 23, 'A' => array ('s' => 123, array (1, 2, 'S '=> "asdasdgsadggsadg ")));
Echo'

';
print_r (array_get_by_key($r, 's'));
/*
result:
Array
(
[0] => 23
[1] => 123
[2] => "asdasdgsadggsadg"
)
*/

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.