开发者

Find the right value in recursive array

I have an array with multiple sub-arrays like this:

    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
                           )

I need a function to find the right Page_URL so if you know the $url is "funds/strategy/a-class-fund" I need to pass that to a function that returns a single array result (which would be the Page_ID = 7 array in this example).

Having a bit of a stupid day, any help would be appreciated!


RecursiveArrayIterator to the rescue:

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;
}


An example of doing the recursion manually (i.e. Not using library functions). If you can use the SPL I would recommend Gordon's 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜