首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >php从数组中获取子数组

php从数组中获取子数组
EN

Stack Overflow用户
提问于 2017-07-02 14:04:53
回答 1查看 100关注 0票数 1

下面是一个数组,我想提取"category_input“作为子数组。这个子数组包含"cat_id“和"post-titles”。然后,我将想插入到WordPress DB下的每个类别的帖子。是。cat_20将有名为post-in-cat-20的帖子,依此类推。

代码语言:javascript
运行
复制
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
        )

)

预期结果

提取的数组将为

代码语言:javascript
运行
复制
    [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数组。

代码语言:javascript
运行
复制
    [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
    )
EN

回答 1

Stack Overflow用户

发布于 2017-07-02 14:16:49

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

$main_arr[category_input];从主数组返回的数组

代码语言:javascript
运行
复制
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中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44867934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档