2015-06-26 20 views
-3

我想在PHP中使用Code = 10720,但我不能。謝謝。PHP Json數組中的數組

{ 
    "OrderId":"6597943", 
    "ExtOrderId":"ExtOrderId0", 
    "MerchantPosId":"KAMPUSHG", 
    "Status":{ 
     "StatusCode":"OPENPAYU_ERROR_VALUE_INVALID", 
     "Code":"10720", 
     "CodeLiteral":"TRANSACTION_INVALID_PARAMS", 
     "Location":"InstallmentPayment", 
     "StatusDesc":["INVALID_CARD_NO","INVALID_CARD_EXPIRATION_DATE","INVALID_CARD_CVV"] 
    } 
} 
+3

'json_decode()'可以幫助.. –

回答

1
$json = '{ 
      "OrderId":"6597943", 
      "ExtOrderId":"ExtOrderId0", 
      "MerchantPosId":"KAMPUSHG", 
      "Status":{ 
       "StatusCode":"OPENPAYU_ERROR_VALUE_INVALID", 
       "Code":"10720", 
       "CodeLiteral":"TRANSACTION_INVALID_PARAMS", 
       "Location":"InstallmentPayment", 
       "StatusDesc":["INVALID_CARD_NO"] 
      } 
     }'; 

$ar = json_decode($json,true); # json-string to array 

# PHP Version < 7 
$code = isset($ar['Status']['Code'])?$ar['Status']['Code']:'default value'; 

# PHP Version >= 7 
$code = isset($ar['Status']['Code'])??'default value'; 
+0

當您使用json_decode你必須**注意**第二個參數和第四個參數:true來獲得數組格式,false爲獲取對象格式:) – PHPJungle

+0

另請注意第四個參數:如果你想獲得** big-int-string **,你需要** JSON_BIGINT_AS_STRING ** @see [默認是在使用json_decode時將大型浮點數轉換爲浮點數](http ://php.net/manual/en/function.json-decode.php) – PHPJungle

+0

_「你可以成爲一名職業球員,你總會有一個12歲的中國孩子比你更好。」_(開玩笑) – Alex

0

使用json_decode()http://php.net/manual/en/function.json-decode.php

$array = '{"OrderId":"6597943","ExtOrderId":"ExtOrderId0","MerchantPosId":"KAMPUSHG","Status":{"StatusCode":"OPENPAYU_ERROR_VALUE_INVALID","Code":"10720","CodeLiteral":"TRANSACTION_INVALID_PARAMS","Location":"InstallmentPayment","StatusDesc":["INVALID_CARD_NO","INVALID_CARD_EXPIRATION_DATE","INVALID_CARD_CVV"]}}'; 


$decode = json_decode($array); 
echo $decode->Status->Code; // Output 10720 

如果你想要一個數組,而不是一個對象

$decode = json_decode($array, true); 
echo $decode['Status']['Code']; // Output 10720