问题描述
我有一个如下所示的数组,我正在尝试过滤具有特定标签或为空(未设置)的条目.然而,这是行不通的.我想这是因为它是多维的.有人吗?
I have an array like below and i am trying to filter entries that have a certain label or that are empty (not set). This is however not working. I guess it is do the fact that it is multi-dimensional. Anyone?
我的数组:
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>';
}
所以我试图过滤掉数组中的最后 2 个条目,并过滤掉任何具有空标签或空值的条目.我在一个我想在我的 wordpress 安装中使用的函数中执行此操作.谁能帮帮我?
So I am trying to filter out the last 2 entries within the array and also filter out any entries that have an empty label or an empty value. I am doing this within a function that i want to use in my wordpress installation. Who can help me out?
推荐答案
Array_filter 循环遍历旧数组并仅返回将返回 true 的结果.您的单个元素将如下所示:
Array_filter loops over the old array and returns only the results that will return true.Your single element will look like this:
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['label'],而是 $el['props']['label'].
Which means that instead of $el['label'], you need to do $el['props']['label'].
//循环内部选项数组
foreach ($el['props']['options'] as $sub){
if (empty($sub['value'])) //save the return value?
}
这篇关于过滤多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!