2015-12-10 70 views
0

我对这个网站完全陌生,所以如果我的文章格式不正确,我很抱歉。如何从多维json数组中获取数值

无论如何,我有我的期望是一个相当简单的问题。我从一个“请求主体” - 阵列,啄提取值,我得到了多数什么,我需要使用这4行代码:

$payment_id = strval($callback_json->id); 
$order_id = strval($callback_json->order_id); 
$currency = strval($callback_json->currency); 
$card_brand = strval($callback_json->metadata->brand); 

我现在的问题是,我已经用完了人才当试图获得似乎是“操作”的“子变量”的“数量”值时。

我试着做这样的,但他们都没有工作:

$amount_total = strval($callback_json->operations[amount]); 
$amount_total = strval($callback_json->operations->amount); 

所以我的问题是现在;我如何格式化这行以获得值“69500”。

我真的很希望有人能帮助我! :-)

{ 
    "id":9256797, 
    "order_id":"23322651466", 
    "accepted":true, 
    "type":"Payment", 
    "text_on_statement":null, 
    "branding_id":null, 
    "variables":{}, 
    "currency":"DKK", 
    "state":"new", 
    "operations":[{ 
     "id":1, 
     "type":"authorize", 
     "amount":69500, 
     "pending":false, 
     "qp_status_code":"20000", 
     "qp_status_msg":"Approved", 
     "aq_status_code":"20000", 
     "aq_status_msg":"Approved", 
     "data":{}, 
     "callback_url":"http://requestb.in/105y8k81", 
     "callback_success":null, 
     "callback_response_code":null, 
     "created_at":"2015-12-05T12:40:40+00:00" 
     }], 

"metadata":{ 
    "type":"card", 
    "brand":"visa", 
    "last4":"0008", 
    "exp_month":11, 
    "exp_year":2016, 
    "country":"DNK", 
    "is_3d_secure":false, 
    "hash":"6f976a4e388928beb4ad3OrQHCS2LDGNAFZVK3i54p6q8heV0RRci", 
    "number":null, 
    "customer_ip":"2.110.77.40", 
    "customer_country":"DK", 
    "fraud_suspected":false, 
    "fraud_remarks":[] 
} 

回答

1

使用

$amount_total = strval($callback_json->operations[0]->amount); 

[因为在JSON为阵列的开放标签。

{'foo':[{'bar':"A"},{'bar':"B"}]} 
$val->foo[0]->bar; # A 
$val->foo[1]->bar; # B 

希望有帮助。

+0

哇,这是快速f * ck:D它的工作......谢谢吨,男人! :-) – spazoid