Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从数组中获取子数组_Php_Arrays_Wordpress - Fatal编程技术网

php从数组中获取子数组

php从数组中获取子数组,php,arrays,wordpress,Php,Arrays,Wordpress,这是一个数组,我想提取“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] => Ar

这是一个数组,我想提取“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
    )
然后,我需要制作caegory和posts数组,以便在wp查询中使用

    [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
    )

当您想要更新数组的部分时,请查看给定的部分

通过
$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;
    }
}
请看完整的示例:


现在您可以将
$out\u arr
再次放入
$main\u arr

请添加期望的结果,其中一个是您想要的输出??第二个??是的第二个结果核对答案让我测试一下兄弟!弗雷恩!我想我把你弄糊涂了。首先,我希望得到索引“category\u input”的子数组。通过使用
$main\u arr[category\u input]
可以得到子数组@Frayne Konok,非常感谢。它工作得很好。谢谢我已经对它进行了测试,并得到了预期的结果。