我有一个json对象,我想使用php在循环中访问它。有没有办法可以在循环中访问它而不解码它?请帮忙。这是我的代码。
for($i = 0; $i < count($purchases); $i++){ ?>
<tr class="tr-purchase">
<td>{{ $purchases[$i]['purchase_orders'] }}</td>
</tr>
}?>
{ “ID”:35, “po_code”: “PMS201635”, “purchase_orders”: “一个:2:{I:0;一个:7:{S:2:\” ID \“; S:1 :\ “9 \”; S:3:\ “数量\”; S:1:\ “2 \”; S:4:\ “单位\”; S:7:\ “胶囊\”; S:6 :\“pharma \”; s:12:\“Ibuprofen IB \”; s:9:\“packaging \”; s:7:\“50 caps \”; s:5:\“price \”; s :2:\ “23 \”; S:5:\ “总\”; S:2:\ “46 \”;} I:1;一个:7:{S:2:\ “ID \”; S :2:\ “11 \”; S:3:\ “数量\”; S:1:\ “5 \”; S:4:\ “单位\”; S:6:\ “小品\”; S :6:\“pharma \”; s:12:\“Ecotrin oral \”; s:9:\“packaging \”; s:6:\“100pcs \”; s:5:\“price \”; S:2:\ “13 \”; S:5:\ “总\”; S:2:\ “65 \”;}}”, “freight_charge”: “0”, “overall_total”: “111”, “created_at”:“2016-05-21 16:50:49”,“updated_at”:“2016-05-21 16:50:49”,“shipped_via”:“选择”,“条款”:“”} < / p>
我只想访问对象内的一些值。我该如何访问它?谢谢。
答案 0 :(得分:2)
以下是您如何获得制药的示例,如果您需要更多帮助,请告知我们。 Online Check
首先,您需要json_deocde
json字符串,并且要获取purchase_orders
其他序列化数据,请使用unserialize
并再次获取该数组并访问它。
$json = '{"id":35,"po_code":"PMS201635","purchase_orders":"a:2:{i:0;a:7:{s:2:\\"id\\";s:1:\\"9\\";s:3:\\"qty\\";s:1:\\"2\\";s:4:\\"unit\\";s:7:\\"Capsule\\";s:6:\\"pharma\\";s:12:\\"Ibuprofen IB\\";s:9:\\"packaging\\";s:7:\\"50 caps\\";s:5:\\"price\\";s:2:\\"23\\";s:5:\\"total\\";s:2:\\"46\\";}i:1;a:7:{s:2:\\"id\\";s:2:\\"11\\";s:3:\\"qty\\";s:1:\\"5\\";s:4:\\"unit\\";s:6:\\"Pieces\\";s:6:\\"pharma\\";s:12:\\"Ecotrin oral\\";s:9:\\"packaging\\";s:6:\\"100pcs\\";s:5:\\"price\\";s:2:\\"13\\";s:5:\\"total\\";s:2:\\"65\\";}}","freight_charge":"0","overall_total":"111","created_at":"2016-05-21 16:50:49","updated_at":"2016-05-21 16:50:49","shipped_via":"Select","terms":""}';
$result = json_decode ($json);
$arr = unserialize ($result->purchase_orders);
foreach($arr as $val){
echo '<pre>';
print_r($val);
}
指定该值,让mg的制造商是Ibuprofen IB
,使用if条件。
if($val['pharma'] == 'Ibuprofen IB'){
echo 'Yes you are in "Ibuprofen IB"';
}
答案 1 :(得分:2)
首先,您必须通过调用&#34; json_decode($ jsonData)&#34;将JSON数据转换回本机PHP对象。结果将是一个PHP对象,如下所示:
<?php
// CREATE AN ARRAY TO HOLD THE REAL PHP OBJECT
// DECODED FROM THE JSON DATA
$arrPurchases = json_decode($purchases);
?>
现在您拥有阵列中的所有购买。每个单独的Key的值对应一个PHP对象,您现在可以通过新的循环访问它,如此...
<?php
foreach($arrPurchases as $i=>$objPurchase){ ?>
<tr class="tr-purchase">
// SINCE purchase_orders IS A SERIALIZED ARRAY
// YOU STILL HAVE TO UNSERIALIZE IT TO GET THE DATA YOU WANT....
// BUT YOU MAY JUST var_dump() IT TO SEE THAT YOU ARE ON TRACK...
<?php var_dump($objPurchase->purchase_orders); ?>
<!-- UNSERIALIZE THE DATA TO GET WHAT YOU NEED -->
<?php $data = unserialize($objPurchase->purchase_orders)); ?>
<?php // RESULT OF THE UNSERIALIZE WILL BE AN ARRAY... ?>
<?php $output = ""; foreach($data as $iDex=>$arrObj): ?>
<td>
<!-- START WORKING WITH THE ARRAY: $arrObj -->
<!-- BUILD UP YOUR STRING OUTPUT THEREFROM & THE ECHO IT WITHIN THE <TD> -->
<?php //$output .= $arrObj[]; ?>
<?php // ECHO THE RESULTING OUTPUT HERE; ?>
</td>
<?php endforeach; ?>
</tr>
<?php }?>