duanmei1694 2015-08-13 13:14
浏览 147

在PHP中使用foreach解析JSON - 使用变量外部循环

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

  • 写回答

1条回答 默认 最新

  • dtueufe82643 2015-08-13 14:09
    关注

    You are looking for json_decode. http://php.net/manual/en/function.json-decode.php

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?