如何获取值为[existence] == 1
如果[existence] == 1
我希望获得具有[existence] == 1
并删除所有[existence] == 0
这是我的阵列。
Array
(
[0] => Array
(
[id] => 3
[accountcode_naisen] =>
[extentype] => 0
[extenrealname] =>
[name] => 0090000270
[extenktaiemail] =>
[secret] => Myojyo42_f
[username] => 0090000270
[guestIpAddr] => 192.168.236.15
[participantSetting] => Array
(
[id] => 13
[existence] => 1
[leader] => 1
[simultaneous] =>
)
)
)
这是我目前的代码
foreach ($participants as $participant=>$c) {
if ($c['existence'] != 1) {
unset($participants[$participant]);
}
}
我收到此错误消息**********
[30-Sep-2015 15:42:38] PHP Notice: Use of undefined constant participantSetting - assumed 'participantSetting' in index.php on line 253
答案 0 :(得分:0)
你可以使用比循环更有效的东西。您可以将array_filter()
与匿名函数一起用作回调。像这样的东西
$participants = array_filter(
$participants,
function($element) { return $element['participantSetting']['existence'] == 1; }
);
答案 1 :(得分:0)
我认为这是有效的
foreach ($participants as $participant=>$c) {
if ($c['participantSetting']['existence'] != 1) {
unset($participants[$participant]);
}
}
答案 2 :(得分:-1)
以下代码可能对您有帮助..
foreach ($participants as $key => &$participant)
{
if ($participant['existence'] != 1)
{
unset($participant[$key]);
}
}
在上述迭代后打印数组时,您将获得预期的输出。