douzhengnao8265 2016-02-02 19:04
浏览 80
已采纳

filter_input_array在多维数组上

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条回答 默认 最新

  • 啊啊啊小孔 2016-02-02 20:20
    关注

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

报告相同问题?

悬赏问题

  • ¥15 php的curl不支持http2
  • ¥15 为什么我按照电路图做出的仿真和实物都不能使用
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多