0

I have the following array from a result from var_export( $post_meta ); from $post_meta = get_post_meta( 80 );

array (
  '_edit_last' => 
  array (
    0 => '1',
  ),
  '_edit_lock' => 
  array (
    0 => '1451326767:1',
  ),
  '_sidebar' => 
  array (
    0 => 'Kies Sidebar',
  ),
  '_wp_page_template' => 
  array (
    0 => 'page-pop.php',
  ),
  'custom_sidebar_per_page' => 
  array (
    0 => 'default',
  ),
  '_cat_id' => 
  array (
    0 => '21',
  ),
  '_order_by' => 
  array (
    0 => 'date',
  ),
  '_asc' => 
  array (
    0 => 'DESC',
  ),
  '_post_count' => 
  array (
    0 => '5',
  ),
  '_days' => 
  array (
    0 => '0',
  ),
  '_custom_sidebar_per_page' => 
  array (
    0 => 'default',
  ),
)

Now I need to filter a few of those values if they exist so I can safely use them. I do the following

$args = [
    '_cat_id' => [
        0 => [
            'filter' => FILTER_VALIDATE_INT,
            'default' => 1
        ] 
    ],         
    '_page_title' => [
        0 => FILTER_SANITIZE_STRING,
    ],
    '_posts_title' => [
        0 => FILTER_SANITIZE_STRING,
    ],
    '_order_by' => [
        0 => [
            'filter'  => FILTER_SANITIZE_STRING,
            'default' => 'date'
        ]
    ],
    '_asc' => [
        0 => [
            'filter'  => FILTER_SANITIZE_STRING,
            'default' => 'DESC'
        ]
    ],
    '_post_count' => [
        0 => [
            'filter'  => FILTER_VALIDATE_INT,
            'default' => get_option( 'posts_per_page' )
        ]  
    ]
];  
$meta = filter_var_array( $post_meta, $args );

but I get the following results from var_export( $meta )

array (
  '_cat_id' => false,
  '_page_title' => NULL,
  '_posts_title' => NULL,
  '_order_by' => false,
  '_asc' => false,
  '_post_count' => false,
)

Something like _cat_id should return something like

'_cat_id' => 
  array (
    0 => 21,
  ),

in the resultant array.

Any ideas on how to use filter_var_array on a multidimensional array

1

1 Answer 1

2

You could first "unnest" your array $post_meta, by popping the element out of each sub-array with the use of array_map and array_pop:

$post_meta_flat = array_map('array_pop', $post_meta);

This array $post_meta_flat will look like this:

array (
  '_edit_last' => '1',
  '_edit_lock' => '1451326767:1',
  '_sidebar' => 'Kies Sidebar',
  '_wp_page_template' => 'page-pop.php',
  'custom_sidebar_per_page' => 'default',
  '_cat_id' => '21',
  '_order_by' => 'date',
  '_asc' => 'DESC',
  '_post_count' => '5',
  '_days' => '0',
  '_custom_sidebar_per_page' => 'default',
)

And now this should work:

$meta = filter_var_array( $post_meta_flat, $args );

You can of course do both in a one-liner:

$meta = filter_var_array( array_map('array_pop', $post_meta), $args );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.