简体   繁体   English

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

[英]PHP: Get Value from String within Array

I have the following array: 我有以下数组:

["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"

And I'm trying to get the values from ["details"] separately (type, maskedCC, expirationDate) 我正在尝试分别从["details"]获取值(类型,maskedCC,expirationDate)

Using $details = $card->getData('details'); 使用$ details = $ card-> getData('details'); I'm able to narrow it down to: 我可以将其范围缩小到:

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

From there, I'm not sure how to get the values AE, 0005, 05/2020. 从那里,我不确定如何获取值AE,0005、05 / 2020。

You're dealing with a jsonObj. 您正在处理jsonObj。 You've to decode it to array first. 您必须先将其解码为数组。 $key will be the attribute you want to get like (type, maskedCC, expirationDate) $ key将是您想要获得的属性(类型,maskedCC,expirationDate)

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

Since it's a string and looks to be valid JSON, you can just decode it: 由于它是一个字符串,并且看起来是有效的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.';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM