I am struggling a little bit with a foreach in PHP, I am able to collect the data I want, display it how I need, sort of. Basically I am building a web app, it pulls JSON Data from a Woocommerce store, I am putting the data into an array using foreach and it works fine, however I cannot use the variable created outside of the foreach loop.
I am looking to use it outside the loop because there are multiple woocommerce stores providing the same data, I need to mix them and sort the results in a list, hence needing the ability to put the foreach (or resulting data) into a variable.
I am pretty certain I am missing something silly here, I have read lots of varying posts, nothing that is quite what I am trying to achieve.
Here is my code:
foreach($result['orders'] as $item) {
$ordernum = $item['order_number'];
$firstname = $item['shipping_address']['first_name'];
$lastname = $item['shipping_address']['last_name'];
$company = $item['shipping_address']['company'];
$ad1 = $item['shipping_address']['address_1'];
$ad2 = $item['shipping_address']['address_2'];
$city = $item['shipping_address']['city'];
$state = $item['shipping_address']['state'];
$postcode = $item['shipping_address']['postcode'];
$country = $item['shipping_address']['country'];
$total = $item['total'];
$product = $item['line_items'][0]['name'];
$quantity = $item['line_items'][0]['quantity'];
$size = $item['line_items'][0]['meta'][0]['value'];
$array = "$ordernum<br />$firstname $lastname<br />$company<br /> $ad1<br /> $ad2v $city<br /> $state<br /> $postcode<br /> $country<br /> $quantity $product $size";
echo "<br />";
echo "$array";
}
I would really like to use the variables created within, outside the loop, I need the data coming out of each of them on various other parts of the web app. If someone could help it would be much appreciated, my PHP learning is not fast enough to keep up with what I am trying to do! ;)
Cheers