2016-11-15 34 views
0

我需要遍歷多維數組幷包裝陣列中數組類型的每個元素。具有動態深度的步行陣列

實例陣列:

Array 
(
    [product_id] => 1 
    [product_name] => Jack Daniel's 
    [sku] => 0 
    [size] => 700 
    [case_size] => 6 
    [description] => A spirit from USA 
    [status] => ACTIVE 
    [created] => 2016-10-02 23:13:17 
    [modified] => 2016-10-02 23:13:17 
    [packs] => Array 
     (
      [product_pack_id] => 1 
      [store_id] => 1 
      [product_id] => 1 
      [pack_size] => 1 
      [created] => 2016-10-02 23:13:17 
      [modified] => 2016-10-02 23:13:17 
      [barcodes] => Array 
       (
        [product_barcode_id] => 1 
        [product_id] => 1 
        [store_id] => 1 
        [product_pack_id] => 1 
        [barcode] => 82184045954 
        [created] => 2016-09-29 06:48:54 
        [modified] => 2016-09-29 06:48:54 
       ) 

     ) 

) 

但該陣列的深度可以在3個陣列深至無限變化。

我需要包裝每個n深度在陣列中,例如包=>需要被包裹在一個陣列,但也包=>條形碼需要被包裹在一個陣列給我以下結果:

Array 
(
    [product_id] => 1 
    [product_name] => Jack Daniel's 700ml 
    [sku] => 0 
    [size] => 700 
    [case_size] => 6 
    [description] => 
<p>Jack Daniel's is a sour mash charcoal filtered American whiskey, which makes it different to it cousin, Bourbon. The&nbsp;mash is made up of 80% corn, 12% rye and 8% malt. Then filtered through 10 feet of charcoal to mellow out the flavours of the malt and the corn, which gives it a distinctive smoky flavour.</p> 
    [status] => ACTIVE 
    [created] => 2016-10-02 23:13:17 
    [modified] => 2016-10-02 23:13:17 
    [packs] => 
     [0] => Array 
      (
       [product_pack_id] => 1 
       [store_id] => 1 
       [product_id] => 1 
       [pack_size] => 1 
       [created] => 2016-10-02 23:13:17 
       [modified] => 2016-10-02 23:13:17 
       [barcodes] => 
        [0] => Array 
         (
          [product_barcode_id] => 1 
          [product_id] => 1 
          [store_id] => 1 
          [product_pack_id] => 1 
          [barcode] => 82184045954 
          [created] => 2016-09-29 06:48:54 
          [modified] => 2016-09-29 06:48:54 
         ) 

      ) 

) 

但是數組的深度是可變的,例如上面的深度爲3,但是它明天可以增長到4的深度。

+0

所有你想要做的就是用單個元素'[array]'替換每個嵌套關聯'數組'或者可能涉及到一些聚合? – shudder

+0

喲狗,我聽說你喜歡數組,所以這裏有一個數組數組,以便您可以排列數組,同時排列數組。 - 但是,嚴肅地說,你能澄清你的意思嗎? – Jhecht

+0

[有沒有辦法在不知道深度的情況下遍歷多維數組?](http://stackoverflow.com/questions/10928993/is-there-a-way-to-loop-through-a-多維數組不知道它的dep) –

回答

1

很容易用遞歸求解。未經測試的代碼如下,但這應該會給你一個好主意。

function wrapArrays($array) { 
    $wrappedArray = array(); 
    foreach($array as $k => $v) { 
     if(is_array($v)) $wrappedArray[$k] = array(wrapArrays($v)); 
     else $wrappedArray[$k] = $v; 
    } 
    return $wrappedArray; 
} 

這裏的想法是要經過你的數組的第一個層次,如果任何元素是數組,請通過同樣的方式,陣列,並繼續下去,下去,直到在每個級別每個元素被處理,無論陣列有多深。

+0

完美,謝謝你太多了...我一直在盲目地看着這個小時,這甚至沒有跨過我的腦海! – joshualawson

-2

按照機制。

<?php 

$result = array(); 

while(data available){ // loop for getting all available data 
    $result['product_id'] = 'value'; 
    $result['product_name'] = 'value'; 
    $result['sku'] = 'value'; 
    . 
    . 
    . 
    . 
    . 
    . 
    $result['packs'][]['product_pack_id'] = 'value'; 
    $result['packs'][]['store_id'] = 'value'; 
    $result['packs'][]['product_id'] = 'value'; 
    . 
    . 
    . 
    . 
    $result['packs'][]['barcodes'][]['product_barcode_id'] = 'value'; 
    $result['packs'][]['barcodes'][]['product_id'] = 'value'; 
    . 
    . 
    . 
    . 

} 

print_r($result); 

?> 
+0

鑰匙不會總是相同....試圖使這儘可能動態 – joshualawson

+0

你能解釋我更多嗎?其實你想要什麼? –

+0

這是一個很差的實現。該功能需要儘可能通用。 – jonogilmour