下面是一个数组,我想提取"category_input“作为子数组。这个子数组包含"cat_id“和"post-titles”。然后,我将想插入到WordPress DB下的每个类别的帖子。是。cat_20将有名为post-in-cat-20的帖子,依此类推。
Array
(
    [post_type] => post
    [post_status] => publish
    [content_texarea] => 
    [category_input] => Array
        (
            [0] => cat_20
            [1] => post-in-cat-20
            [2] => post-in-cat-20
            [3] => post-in-cat-20
            [4] => cat_19
            [5] => post-in-cat-19
            [6] => post-in-cat-19
            [7] => post-in-cat-19
            [8] => post-in-cat-19
            [9] => cat_2
            [10] => post-in-cat-2
            [11] => post-in-cat-2
            [12] => post-in-cat-2
        )
)预期结果
提取的数组将为
    [category_input] => Array
    (
        [0] => cat_20
        [1] => post-in-cat-20
        [2] => post-in-cat-20
        [3] => post-in-cat-20
        [4] => cat_19
        [5] => post-in-cat-19
        [6] => post-in-cat-19
        [7] => post-in-cat-19
        [8] => post-in-cat-19
        [9] => cat_2
        [10] => post-in-cat-2
        [11] => post-in-cat-2
        [12] => post-in-cat-2
    )然后,我需要在wp查询中使用caegory和post数组。
    [category_input] => Array
    (
        [cat_20] => Array
            [1] => post-in-cat-20
            [2] => post-in-cat-20
            [3] => post-in-cat-20
        [cat_19] => Array
            [5] => post-in-cat-19
            [6] => post-in-cat-19
            [7] => post-in-cat-19
            [8] => post-in-cat-19
        [cat_2] => Array
            [10] => post-in-cat-2
            [11] => post-in-cat-2
            [12] => post-in-cat-2
    )发布于 2017-07-02 14:16:49
当您想要更新数组的一部分时,请看一下给定的部分。
由$main_arr[category_input];从主数组返回的数组
foreach($category_input as $val){
    $part = explode('_', $val);
    if(isset($part[0]) && $part[0] == 'cat'){
        $out_arr[$val] = array();
        $key = $val;
    }else{
        $out_arr[$key][] = $val;
    }
}查看完整的示例:https://3v4l.org/JI9U7
现在,您可以将$out_arr再次放到$main_arr中。
https://stackoverflow.com/questions/44867934
复制相似问题