2016-09-24 136 views
-1

我有具有鍵值解析陣列串

Array ([0] =>stdClass Object ([privacy] => {"name": "on", "email": null, "mobile": null, "address": "on", "alt_contact": "on"})) 

我需要打印其中將包含逗號分隔鍵具有值「上」的字符串的對象陣列。結果字符串應該是

$result = "name,address,alt_contact"; 

請幫

+0

希望這是一個重複的問題。檢查使用此鏈接的答案http://stackoverflow.com/questions/21168422/how-to-access-a-property-of-an-object-stdclass-object-member-element-of-an-arr –

回答

0

這會做的伎倆你..

$json = '{"name": "on", "email": null, "mobile": null, "address": "on", "alt_contact": "on"}'; 
$array = json_decode($json,true); 

$str = ""; 
foreach($array as $key=>$value) 
{ 
    if($value == "on") 
     $str .= $key.","; 
} 
return rtrim($str,","); 

結果: - 姓名,地址,alt_contact

0

這就是您的請求轉換爲PHP的方式:

我需要

這是直接從上面的列表中的代碼:

$json = '{"name": "on", "email": null, "mobile": null, "address": "on", "alt_contact": "on"}'; 
// Pass TRUE as second argument to get arrays back; stdObject is useless 
$data = json_decode($json, TRUE); 

// print comma separated keys of 'on' 
echo(implode(',', array_keys($data, 'on'))); 
0

感謝Jaimin,它幫助

必須得到一個數組解析的$隱私對象

$userPrivacies = $userPrivacies[0]->privacy; 

後來使用你的代碼。

使溶液變成

$userPrivacies = $userPrivacies[0]->privacy; 

$array = json_decode($userPrivacies,true); 

$str = ""; 
foreach($array as $key=>$value) 
{ 
    if($value == "on") 
     $str .= $key.","; 
} 
echo rtrim($str,","); 
+0

如果我的答案爲你工作,你應該選擇我接受的答案..它是一種在stackoverflow上的欣賞方式... – Jaimin