How to obtain the key value of an array

Source: Internet
Author: User
A multi-dimensional array arr and another array key [1, 2] are known. The first element in the key represents the first latitude of the arr array, and the second element represents the second latitude of arr, similarly, the number of key elements is unknown. how to obtain the pairs listed by the key corresponding to arr... A multi-dimensional array arr and another array key = [1, 2] are known. The first element in the key represents the first latitude of the arr array, and the second element represents the second latitude of arr, similarly, the number of key elements is unknown. how to obtain the values listed by the key corresponding to arr,
For example, if the above key is 1, 2], I will obtain the arr [1, how key = 1, 2, 4]. I want to obtain the arr [1 [4]...

Reply content:

A multi-dimensional array arr and another array key = [1, 2] are known. The first element in the key represents the first latitude of the arr array, and the second element represents the second latitude of arr, similarly, the number of key elements is unknown. how to obtain the values listed by the key corresponding to arr,
For example, if the above key is 1, 2], I will obtain the arr [1, how key = 1, 2, 4]. I want to obtain the arr [1 [4]...


  "0", "children" => [["value" => "0-0",], ["value" => "0-1 ", "children" => [["value" => "0-1-0",], ["value" => "1 ", "children" => [["value" => "1-0", "children" => [["value" => "1-0-0 ", "children" => [["value" => "1-0-0-0 ", "children" => [],]; $ key = [0, 1]; function getValueByKey ($ arr, $ key) {$ index = array_shift ($ key); if (count ($ key) = 0) return $ arr [$ index]; return getValueByKey ($ arr [$ index] ['Children '], $ key);} // change the valuefunction setValueByKey (& $ arr, $ key, $ value) of the corresponding index) {$ index = array_shift ($ key); if (count ($ key) = 0) return $ arr [$ index] ['value'] = $ value; return setValueByKey ($ arr [$ index] ['Children '], $ key, $ value);} setValueByKey ($ arr, $ key, '123 '); // return the matched data group. then, let's look at what you want to fetch. The value is print_r (getValueByKey ($ arr, $ key) ['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.