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

过滤多维数组

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?
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败