0

I need to walk through a multidimensional array and wrap each element that is of type array in an array.

example array:

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
                )

        )

)

But the depth of the array can change from 3 arrays deep to unlimited.

I need to wrap each n depth in an array, for example packs => needs to be wrapped in an array but also packs => barcodes needs to be wrapped in an array to give me the following result:

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
                        )

            )

)

But the depth of the array is variable, for instance the above has a depth of 3 but it could grow to a depth of 4 tomorrow.

Joshua Lawson
  • 376
  • 3
  • 15
  • All you want to do is to replace each nested associative `array` with single element `[array]` or some aggregation might be involved? – shudder Nov 15 '16 at 04:09
  • Yo dog, I heard you liked arrays, so here's an array of arrays so that you can array while arraying your arrays. -- but seriously, can you clear up what you mean? – Jhecht Nov 15 '16 at 04:12
  • Possible duplicate of [Is there a way to loop through a multidimensional array without knowing it's depth?](http://stackoverflow.com/questions/10928993/is-there-a-way-to-loop-through-a-multidimensional-array-without-knowing-its-dep) – Khorshed Alam Nov 15 '16 at 04:13
  • I don't know how else to explain besides the examples above. pretty much every nested array I want to nest or wrap in another array. – Joshua Lawson Nov 15 '16 at 04:15
  • I mimic the comments above - this achieves nothing but to give you an unnecessary extra array around a single element. – jonogilmour Nov 15 '16 at 04:31

2 Answers2

2

Pretty easily solved with recursion. Untested code below, but this should give you a good idea.

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

The idea here is to go through the first level of your array, and if any elements are arrays, go through that array in the same way, and keep going and going until every element at every level is processed, no matter how deep the array is.

jonogilmour
  • 663
  • 5
  • 9
  • perfect, thank you soo much... I have been blindly looking at this for hours and this didn't even cross my mind! – Joshua Lawson Nov 15 '16 at 04:36
  • Thank you, look like this snippet works in general for multidimensions arrays. it works fine for me inside class using method. Thank you again. – Tariq Ahmed Dec 08 '20 at 10:59
-2

Follow the mechanism.

<?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);

?>
AGM Tazim
  • 2,213
  • 3
  • 16
  • 25