简体   繁体   中英

How to get single value from this PHP array

Example

print_r($a) 

Array ( [Status] => 100 [RefID] => 12345678 [ExtraDetail] => {"Transaction":{"CardPanHash":"0866A6EAEA5CB08B3AE61837EFE7","CardPanMask":"999999******9999"}} ) 

i need to take CardPanMask value

An example: I wrote this code but it didn't work

$cardnumber=$a[ExtraDetail]->Transaction->CardPanMask; 

the $cardnumber must be 999999******9999 but when i echo $cardnumber; but its empty return noting

Your ExtraDetail key is actually a JSON object, which you can't parse with PHP easily without decoding it.

Your $cardnumber variable should be declared as:

$cardnumber = json_decode($a['ExtraDetail'])->Transaction->CardPanMask;

Or:

$cardnumber = json_decode($a['ExtraDetail'], true)['Transaction']['CardPanMask'];

If you plan on needing multiple values from the $a['ExtraDetail'] key, you may consider decoding the entire value into it's own value first.

//you can use `true` as the second parameter of `json_decode()` if you want it to decode as an array instead of an object.
$transaction = json_decode($a['ExtraDetail'])->Transaction;
$cardnumber = $transaction->CardPanMask;

try this:

$a = [
    'Status' => 100,
    'RefID' => 12345678,
    'ExtraDetail' => json_decode ('{"Transaction":{"CardPanHash":"0866A6EAEA5CB08B3AE61837EFE7","CardPanMask":"999999******9999"}}')
];

print_r($a['ExtraDetail']->Transaction->CardPanMask);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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