2016-11-15 46 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