在递归数组中找到正确的值

时间:2010-04-16 13:49:50

标签: php

我有一个包含多个子数组的数组,如下所示:

    Array
   (
       [0] => Array
           (
               [Page_ID] => 1
               [Page_Parent_ID] => 0
               [Page_Title] => Overview
               [Page_URL] => overview
               [Page_Type] => content
               [Page_Order] => 1
           )

       [1] => Array
           (
               [0] => Array
                   (
                       [Page_ID] => 2
                       [Page_Parent_ID] => 1
                       [Page_Title] => Team
                       [Page_URL] => overview/team
                       [Page_Type] => content
                       [Page_Order] => 1
                   )

           )

       [2] => Array
           (
               [Page_ID] => 3
               [Page_Parent_ID] => 0
               [Page_Title] => Funds
               [Page_URL] => funds
               [Page_Type] => content
               [Page_Order] => 2
           )

       [3] => Array
           (
               [0] => Array
                   (
                       [Page_ID] => 4
                       [Page_Parent_ID] => 3
                       [Page_Title] => Strategy
                       [Page_URL] => funds/strategy
                       [Page_Type] => content
                       [Page_Order] => 1
                   )

               [1] => Array
                   (
                       [0] => Array
                           (
                               [Page_ID] => 7
                               [Page_Parent_ID] => 4
                               [Page_Title] => A Class Fund
                               [Page_URL] => funds/strategy/a-class-fund
                               [Page_Type] => content
                               [Page_Order] => 1
                           )

                       [1] => Array
                           (
                               [0] => Array
                                   (
                                       [Page_ID] => 10
                                       [Page_Parent_ID] => 7
                                       [Page_Title] => Information
                                       [Page_URL] => funds/strategy/a-class-fund/information
                                       [Page_Type] => content
                                       [Page_Order] => 1
                                   )

                               [1] => Array
                                   (
                                       [Page_ID] => 11
                                       [Page_Parent_ID] => 7
                                       [Page_Title] => Fund Data
                                       [Page_URL] => funds/strategy/a-class-fund/fund-data
                                       [Page_Type] => content
                                       [Page_Order] => 2
                                   )

                           )

                       [2] => Array
                           (
                               [Page_ID] => 8
                               [Page_Parent_ID] => 4
                               [Page_Title] => B Class Fund
                               [Page_URL] => funds/strategy/b-class-fund
                               [Page_Type] => content
                               [Page_Order] => 2
                           )

我需要一个函数来找到正确的Page_URL所以如果你知道$ url是“funds / strategy / a-class-fund”我需要将它传递给一个返回单个数组结果的函数(这将是本例中的Page_ID = 7数组。)

有点愚蠢的一天,任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:5)

RecursiveArrayIterator救援:

function findByPageUrl($url, array $data) {

    $iterator = new RecursiveIteratorIterator(
                    new RecursiveArrayIterator($data),
                    RecursiveIteratorIterator::SELF_FIRST);

    foreach($iterator as $val) {
        if(is_array($val) && array_key_exists('Page_URL', $val)) {
            if($val['Page_URL'] === $url) {
                return $val;
            }
        }
    }
    return FALSE;
}

答案 1 :(得分:2)

手动执行递归的示例(即不使用库函数)。如果你可以使用SPL我会推荐Gordon的solution

/**
 * Given an array and a key, this finds a sub array where the key contains
 * a value equal to the needle and returns the entire sub array
 *
 * @param $haystack The array containing sub arrays
 * @param $key The key of the item in the sub array
 * @param $needle The item being searched for
 */
function find_parent(array $haystack, $key, $needle){
    //if the array contains the value we want return it
    if ( isset($haystack[$key]) && $haystack[$key] == $needle ){
        return $haystack
    }

    foreach ( $haystack as $v ){
        if ( is_array($v) ){
             $result = find_parent($v, $key, $needle);
             if ( $result !== null ){
                 return $result;
             }
        }

    }

    return null;
}