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

循环遍历数组并创建一个新的查询结果数组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条回答 默认 最新

相关推荐 更多相似问题