2016-12-01 54 views
0

我有以下的JSON字符串使用json_decode:如何在PHP

{"createLead":{"client_id":42891}} 

需要解析這個字符串PHP用於獲取client_id值。 使用以下命令:

$json_result= json_decode('{"createLead":{"client_id":42891}}', true); 

我怎麼使用越來越client_id值響應數組? 下面的命令不起作用,並返回空字符串:

$client_id = $json_result['createLead']['client_id']; 
+0

什麼是[文件](http://php.net/manual/en/function.json -decode.php)說?你的問題的答案已經在那裏了! – Dragos

+0

它也將幫助你檢查'var_dump($ json_result)' – Dragos

+0

該代碼應該工作。在這之後你用'$ client_id'做了什麼? – Chris

回答

-2

使用此代碼

<?php 
$json = '{"createLead":{"client_id":42891}}'; 
$json_result= json_decode($json, true); 
echo $json_result['createLead']['client_id']; 
?> 
Output: 42891 
+0

爲什麼?這與OP發佈的代碼有什麼不同? – Chris

+0

@Chris,沒有區別,上面的代碼只是格式化的。它在PHP 5.2和更高版本中顯示輸出。 – Test1