2012-05-11 91 views
0

我這裏有一個嵌套的多維數組:問題與遞歸循環和嵌套多維數組

Array 
(
[merchant] => Array 
    (
     [XML_Serializer_Tag] => Array 
      (
       [id] => 736 
       [name] => Cadbury Gifts Direct 
      ) 

     [prod] => Array 
      (
       [XML_Serializer_Tag] => Array 
        (
         [0] => Array 
          (
           [XML_Serializer_Tag] => Array 
            (
             [id] => 88966064 
             [pre_order] => no 
             [web_offer] => no 
             [in_stock] => no 
             [stock_quantity] => 0 
            ) 

           [pId] => 608 
           [isbn] => 0000000000000 
           [text] => Array 
            (
             [name] => 50% OFF 56g Creme Egg Minis 
             [desc] => 50% OFF Creme Egg Minis in a 56g bag. 
            ) 

           [uri] => Array 
            (
             [awTrack] => http://www.awin1.com/pclick.php?p=88966064&a=67702&m=736 
             [awThumb] => http://images.productserve.com/thumb/736/88966064.jpg 
             [awImage] => http://images.productserve.com/preview/736/88966064.jpg 
             [mLink] => http://www.cadburygiftsdirect.co.uk/products/608-50-off-56g-creme-egg-minis.aspx 
             [mImage] => http://www.cadburygiftsdirect.co.uk/images/thumbs/0001084.png 
            ) 

           [price] => Array 
            (
             [XML_Serializer_Tag] => Array 
              (
               [curr] => GBP 
              ) 

             [buynow] => 0.31 
             [store] => 0.00 
             [rrp] => 0.00 
             [delivery] => 0.00 
            ) 

           [cat] => Array 
            (
             [awCatId] => 437 
             [awCat] => Chocolate 
             [mCat] => Full Range 
            ) 

           [brand] => 
           [valFrom] => 0000-00-00 
           [valTo] => 0000-00-00 
           [comAmount] => 0.00 
          ) 

片段循環之後。

所以......

[1] => Array 
[2] => Array 
[3] => Array 

等等

我需要找到每個陣列段的屬性的名稱。

所以我已經使用這個遞歸循環:

private function recursive_array($old_array, $new_array = array()) { 

    foreach ($old_array as $key => $value) { 

     if (is_array($value) && $key < 1) { 
      $new_array = $this->recursive_array($value, $new_array); 

     } else { 
      if ($key < 1) { 
       $new_array[] = $key; 

      } 
     } 
    } 
    return $new_array; 
} 

這是輸出:

array 
    0 => string 'id' (length=2) 
    1 => string 'name' (length=4) 
    2 => string 'id' (length=2) 
    3 => string 'pre_order' (length=9) 
    4 => string 'web_offer' (length=9) 
    5 => string 'in_stock' (length=8) 
    6 => string 'stock_quantity' (length=14) 
    7 => string 'pId' (length=3) 
    8 => string 'isbn' (length=4) 
    9 => string 'name' (length=4) 
    10 => string 'desc' (length=4) 
    11 => string 'awTrack' (length=7) 
    12 => string 'awThumb' (length=7) 
    13 => string 'awImage' (length=7) 
    14 => string 'mLink' (length=5) 
    15 => string 'mImage' (length=6) 
    16 => string 'curr' (length=4) 
    17 => string 'buynow' (length=6) 
    18 => string 'store' (length=5) 
    19 => string 'rrp' (length=3) 
    20 => string 'delivery' (length=8) 
    21 => string 'awCatId' (length=7) 
    22 => string 'awCat' (length=5) 
    23 => string 'mCat' (length=4) 
    24 => string 'brand' (length=5) 
    25 => string 'valFrom' (length=7) 
    26 => string 'valTo' (length=5) 
    27 => string 'comAmount' (length=9 

它是什麼,還撿是頂級的嵌套數組:

[XML_Serializer_Tag] => Array 
      (
       [id] => 736 
       [name] => Cadbury Gifts Direct 
      ) 

這只是整個Feed的細節,而不是各個細分市場。

我需要過濾掉頂嵌套的數組,但請記住,細節是動態的,所以按鍵的名稱可以從一個進料改變爲下

回答

2

也許這的一種方式?

$riter = new RecursiveIteratorIterator(New RecursiveArrayIterator($array)); 
foreach ($riter as $key => $val) { 
    if ($riter->getDepth() > 1) { 
     echo "$key => $val\n"; 
    } 
} 

默認情況下,RecursiveIteratorIterator只訪問葉節點(每個路徑下的最深層)。 getDepth()可用於確保最小深度。我不知道,如果1是正確的號碼,但無論如何...

+0

是的,如果我將它設置爲二級上所有的不工作不同類型的數組。我現在需要重寫我的函數,因爲通過使用這種方法,它刪除了我用來計數的密鑰(這會輸出一長串字符串) – AndyW

0

使用array_shift彈出關閉陣列的第一個元素