Looping through nested arrays

[color=#000000][font=Arial,]Having some real problems with this and hoping someone can help. Here's a reduced sample of the data I'm working with. So here $order_info is an array, inside that array theres another array called $items and within that there's multiple arrays which have the item ID as a name (very annoying!). What I'm trying to do is loop through the arrays within the $items array - the problem is I won't know their name. Is it possible to just loop through every array within an array (which is also within an array!)? It also needs to use smarty. I've tried various nested for loops but all have just come back with a blank page, suggesting an error.[/font][/color]


{$order_info} Array (86)
order_id => "15"

items => Array (2)
4008222099 => Array (19)
item_id => "4008222099"
order_id => "15"
product_id => "836"
product_code => "B0001WS6L2"
price => "229.95"
amount => "1"
extra => Array (9)
step => "1"
product_options => Array (0)
unlimited_download => "N"
product => "LINGO TR-2203 Pacifica Talk Talking T..."
company_id => "0"
is_edp => "N"
edp_shipping => "N"
base_price => "229.95"
stored_price => "N"
product => "LINGO TR-2203 Pacifica Talk Talking T..."
deleted_product => ""
discount => "0"
company_id => "0"
base_price => "229.95"
original_price => "229.95"
cart_id => "4008222099"
tax_value => "0"
subtotal => "229.95"
display_subtotal => "229.95"
shipped_amount => "0"
shipment_amount => "1"
1157311813 => Array (19)
item_id => "1157311813"
order_id => "15"
product_id => "744"
product_code => "B00028DM96"
price => "119.99"
amount => "1"
extra => Array (9)
step => "1"
product_options => Array (0)
unlimited_download => "N"
product => "Sharp Electronics PW-E550 Electronic ..."
company_id => "0"
is_edp => "N"
edp_shipping => "N"
base_price => "119.99"
stored_price => "N"
product => "Sharp Electronics PW-E550 Electronic ..."
deleted_product => ""
discount => "0"
company_id => "0"
base_price => "119.99"
original_price => "119.99"
cart_id => "1157311813"
tax_value => "0"
subtotal => "119.99"
display_subtotal => "119.99"
shipped_amount => "0"
shipment_amount => "1"

Not clear what your “looping” is trying to capture. Are you trying to do this in PHP or in the templater?

Generally there are 2 approaches:

  1. If you know the depth, you can do a series of nested foreach() loops.
  2. If you don't know the depth or want a more general approach use a recursive function to “walk” the array. There is a built-in php function called 'array_walk' that you can investigate.

Thanks for the reply. I'm trying to do this within the template. It's basically for an affiliate tracking pixel which requires the name of each product along with price and ID to be captured at the end - I'm struggling to get into the products array inside of the items array because the product array name changes for each product.



Hope that makes a bit more sense of it,

```php

{foreach from=$order_info.items key=“p_key” item=“p_info”}

product code is: {$p_info.product_code}


product name is: {$p_info.product}


{/foreach}
```