PHP:从数组中的字符串获取值

时间:2019-02-08 20:49:02

标签: php

我有以下数组:

["entity_id"] => "1"
["customer_id"] => "18"
["public_hash"] => "xxxxxxxxxx041e6f5246f041429424422795137118223d73bb6197410f9cbc77"
["payment_method_code"] => "braintree"
["type"] => "card"
["created_at"] => "2018-12-19 21:24:13"
["expires_at" => "2020-06-01 00:00:00" "gateway_token"] => "8cx9y7"
["details"] => "{"type":"AE","maskedCC":"0005","expirationDate":"05\\\\\\\\/2020"}"
["is_active"] => "1"
["is_visible"] => "1"

我正试图分别从["details"]获取值(类型,maskedCC,expirationDate)

使用 $ details = $ card-> getData('details'); 我可以将其范围缩小到:

{"type":"AE","maskedCC":"0005","expirationDate":"05\/2020"}

从那里,我不确定如何获取值AE,0005、05 / 2020。

2 个答案:

答案 0 :(得分:3)

您正在处理jsonObj。 您必须先将其解码为数组。 $ key将是您想要获得的属性(类型,maskedCC,expirationDate)

$jsonArray = json_decode($details,true);
echo $jsonArray[$key];

答案 1 :(得分:2)

由于它是一个字符串,并且看起来是有效的JSON,因此您可以对其进行解码:


$details = json_decode($card->getData('details'), true);

if (!empty($details))
{
    //Valid JSON
    echo $details['type'];
    echo $details['maskedCC'];
    echo $details['expirationDate'];
}
else
{
    echo 'The details string was not valid JSON.';
}