2016-09-24 76 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