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