大家好,我只是想问一下如何使用循环获取 JSON 的值?我已经做了一些研究,但我的代码不起作用。我不知道我的错误是什么。我想要的是获取所有 json 值并将其放入表中。顺便说一句,我正在使用 codeigniter
我的模型:
public function getAllPurchaseOrder(){
$array = array();
$sqlSelectAll = "select * from po_order
inner join po_supplier on po_order.poid = po_supplier.poid
inner join po_account_details on po_supplier.poid = po_account_details.poid";
$resultSelectAll = $this->db->query($sqlSelectAll);
foreach($resultSelectAll->result_array() as $details){
$array[] = $details;
}
return "{\"data\":" .json_encode($array). "}";
}
我的控制器:
public function index(){
$data['title'] = "Welcome";
$data['copyright'] = date('Y');
$data['purchased_comment'] = $this->po_model->fetch_purchased_refid();
$data['link'] = "PO";
$data['itemlist'] = $this->po_model->getAllPurchaseOrder();
$this->load->view('common/header_common',$data);
$this->load->view('common/navigation');
$this->load->view('Purchase Order/contents/index',$data);
$this->load->view('common/footer_common',$data);
}
这是我的代码:
echo "<pre>";
var_dump(json_decode($itemlist,true));
我的 json 输出使用 var_dump(json_encode)
array(1) {
["data"]=>
array(1) {
[0]=>
array(25) {
["poid"]=>
string(1) "1"
["itemid"]=>
string(1) "3"
["item_desc"]=>
string(6) "Drinks"
["item_qty"]=>
string(1) "4"
["item_price"]=>
string(4) "5.00"
["total_amount"]=>
string(5) "20.00"
["cash_on_delivery"]=>
string(1) "Y"
["is_check"]=>
string(1) "Y"
["bank_transfer"]=>
string(1) "N"
["transaction_date"]=>
string(19) "2013-08-15 15:44:30"
["posupid"]=>
string(1) "1"
["spid"]=>
string(1) "2"
["spname"]=>
string(12) "MILO COMPANY"
["spaddress"]=>
string(6) "Manila"
["contact_person"]=>
string(4) "None"
["tin"]=>
string(12) "1232-456-789"
["contact_number"]=>
string(7) "0912151"
["poaccid"]=>
string(1) "1"
["payee_name"]=>
string(6) "Larren"
["bank_details"]=>
string(11) "BDO Account"
["account_name"]=>
string(7) "Premium"
["account_number"]=>
string(5) "54564"
["prepared"]=>
string(5) "Admin"
["checked"]=>
string(5) "Admin"
["approved"]=>
string(5) "Admin"
}
}
我的问题是我无法获得 json 的值。例如,假设我想要poid的值,但我不知道如何。我尝试了一些数组但无法正常工作
这是我的代码:
$items = json_decode($itemlist,true);
echo $items['poid'];