Recursive dizideki doğru değeri bulmak

2 Cevap

Ben bu gibi birden fazla alt-dizileri ile bir dizi var:

    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
                           )

Eğer $ url olduğunu biliyorum eğer "fon / strateji / a-class-fon" Ben tek bir dizi sonuç (olurdu page_id = 7 döndüren bir işlev olduğunu geçmesi gerekiyor bu yüzden doğru Page_URL bulmak için bir işlev gerekir Bu örnekte dizi).

Aptal bir gün biraz sahip, herhangi bir yardım mutluluk duyacağız!

2 Cevap

(Yani kütüphane fonksiyonlarını kullanarak değil) elle özyineleme yapmanın bir örneği. Eğer SPL kullanabilirsiniz eğer ben Gordon solution öneriyoruz.

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

RecursiveArrayIterator kurtarmak için:

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