dongzi9196
2017-10-27 12:40
浏览 225
已采纳

过滤多维数组

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?

My array:

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] => 
                )

        )

)

My code:

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>';
}

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?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxi4114 2017-10-27 12:45
    已采纳

    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
                                    )
    
                            )
    
                    )
    
            )
    

    Which means that instead of $el['label'], you need to do $el['props']['label'].

    //Looping internal options array

    foreach ($el['props']['options'] as $sub){
       if (empty($sub['value'])) //save the return value?
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题