dongmeng2687 2014-01-17 18:51 采纳率: 0%
浏览 134
已采纳

循环遍历数组并创建一个新的查询结果数组PHP

I have a PHP array object that can contain zero or more values like this:

Array
(
[0] => stdClass Object
    (
        [id] => dkgasO05P2XpfyWW
    )

[1] => stdClass Object
    (
        [id] => LzE6G9UQIShOUoKq
    )

)

I want to loop through each value in this array and use the id in a query that returns an object that looks like this:

Array
(
    [0] => stdClass Object
        (
            [id] => taWPlKGXHR5Y03cc
            [title] => Test Document Title
            [filename] => test.docx
        )

)

On each iteration of the loop the query returns with one result in the form of an array object. I want to add the object to an array object that in this case would look something like this:

Array
(
    [0] => stdClass Object
        (
            [id] => dkgasO05P2XpfyWW
            [title] => Test Document Title 0
            [filename] => test0.docx
        )
    [1] => stdClass Object
        (
            [id] => LzE6G9UQIShOUoKq
            [title] => Test Document Title 1
            [filename] => test1.docx
        )
)

The query is written and working and I know I need to use a foreach loop to iterate over the array of IDs, but I don't quite get how to set it up so that the end result is an array object as listed just above. I'm using PHP & Codeigniter to do all of this.

The code of the foreach I have so far is something like this:

$child = array();
foreach ($id as $row) {
        $child = $this->users_model->get_docnfo($id);
}

Thanks for reading!

  • 写回答

1条回答 默认 最新

  • dov6891 2014-01-17 19:18
    关注

    You should try it with

    $child = array();
    foreach ($id as $row) {
            $child[] = $this->users_model->get_docnfo($row->id);
    }
    

    Note the $row->id instead of $id and also the brackets after $child.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)