我有一个如下所示的数组,我试图过滤具有特定标签或空(未设置)的条目。然而,这不起作用。我想这是事实,它是多维的。任何人吗?
我的阵列:
Array
(
[0] => Array
(
[id] => app_i-have
[type] => checkbox
[props] => Array
(
[required] => 0
[label] => I have
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => mobile studio
)
[1] => Array
(
[baseline] => 0
[value] => makeup artist
)
)
)
)
[1] => Array
(
[id] => app_customers
[type] => select
[props] => Array
(
[required] => 0
[label] => Customers
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => Private
)
[1] => Array
(
[baseline] => 0
[value] => Business
)
)
)
)
[2] => Array
(
[id] => app_exclude
[type] => select
[props] => Array
(
[required] => 0
[label] => Exclude
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => option 1
)
[1] => Array
(
[baseline] => 0
[value] => option 2
)
)
)
)
[3] => Array
(
[id] => app_exclude-2
[type] => input_text
[props] => Array
(
[required] => 0
[label] => Exclude 2
[tip] =>
)
)
)
我的代码:
function get_listing_cfs() {
global $wpdb;
$serialized=$wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='va_form'");
$array=unserialize($serialized);
echo '<pre>'.print_r($array, true).'</pre>';
$source = array_filter($array, function($el) {
return !(
$el['label'] == 'Exclude' ||
$el['label'] == 'Exclude 2' ||
!isset($el['label']) ||
empty($el['value']) ||
!isset($el['value'])
);
});
echo '<pre>'.print_r($source, true).'</pre>';
}
所以我试图过滤掉数组中的最后两个条目,并过滤掉任何具有空标签或空值的条目。我在我想要在我的wordpress安装中使用的函数中执行此操作。谁可以帮助我?
答案 0 :(得分:1)
Array_filter遍历旧数组并仅返回将返回true的结果。 您的单个元素将如下所示:
Array
(
[id] => app_i-have
[type] => checkbox
[props] => Array
(
[required] => 0
[label] => I have
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => mobile studio
)
[1] => Array
(
[baseline] => 0
[value] => makeup artist
)
)
)
)
这意味着您需要执行$ el [&#39;道具&#39;] [&#39;标签&#39;]而不是$ el [&#39;标签&#39;]。
//循环内部选项数组
foreach ($el['props']['options'] as $sub){
if (empty($sub['value'])) //save the return value?
}