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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?