Filtering multi-dimensional array

RobbTe

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?

vicbyte

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?
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related