在多维数组中搜索值,然后输出数组中的其他值
我想知道如何在下面的数组中搜索关键 Problem_id 和等于我将提供的变量的值。然后,当它找到具有匹配键和变量的数组时,它也会输出数组该部分中的其他值。
例如,使用下面的示例数据。您建议我如何在数组中搜索具有键 Problem_id 和值 3 的所有数组,然后让它输出键 Problem_update_date 的值和键 Problem_update_text 的值。然后继续寻找下一个出现的地方?
预先感谢,我一直在努力寻找答案,并且相信我已经超出了我的能力范围!
print_r($updates);
的输出
CI_DB_mysql_result Object
(
[conn_id] => Resource id #30
[result_id] => Resource id #35
[result_array] => Array()
[result_object] => Array()
[current_row] => 0
[num_rows] => 5
[row_data] =>
)
print_r($updates->result_array());
的输出
Array
(
[0] => Array
(
[problem_update_id] => 1
[problem_id] => 3
[problem_update_date] => 2010-10-01
[problem_update_text] => Some details about a paricular issue
[problem_update_active] => 1
)
[1] => Array
(
[problem_update_id] => 4
[problem_id] => 3
[problem_update_date] => 2010-10-01
[problem_update_text] => Another update about the problem with an ID of 3
[problem_update_active] => 1
)
[2] => Array
(
[problem_update_id] => 5
[problem_id] => 4
[problem_update_date] => 2010-10-12
[problem_update_text] => An update about the problem with an ID of four
[problem_update_active] => 1
)
[3] => Array
(
[problem_update_id] => 6
[problem_id] => 4
[problem_update_date] => 2010-10-12
[problem_update_text] => An update about the problem with an ID of 6
[problem_update_active] => 1
)
[4] => Array
(
[problem_update_id] => 7
[problem_id] => 3
[problem_update_date] => 2010-10-12
[problem_update_text] => Some new update about the problem with the ID of 3
[problem_update_active] => 1
)
)
I'm wondering how I might go about searching the array below for the key problem_id and a value equal to a variable which I would provide. Then, when it finds an array with a the matching key and variable, it outputs the other values in that part of the array too.
For example, using the sample data below. How would you recommend that I search the array for all the arrays that have the key problem_id and the value 3 and then have it output the value of the key problem_update_date and the value of the key problem_update_text. Then keep searching to find the next occurrence?
Thanks in advance, I've been searching really hard for the answer and believe i'm over my head!
Output of print_r($updates);
CI_DB_mysql_result Object
(
[conn_id] => Resource id #30
[result_id] => Resource id #35
[result_array] => Array()
[result_object] => Array()
[current_row] => 0
[num_rows] => 5
[row_data] =>
)
Output of print_r($updates->result_array());
Array
(
[0] => Array
(
[problem_update_id] => 1
[problem_id] => 3
[problem_update_date] => 2010-10-01
[problem_update_text] => Some details about a paricular issue
[problem_update_active] => 1
)
[1] => Array
(
[problem_update_id] => 4
[problem_id] => 3
[problem_update_date] => 2010-10-01
[problem_update_text] => Another update about the problem with an ID of 3
[problem_update_active] => 1
)
[2] => Array
(
[problem_update_id] => 5
[problem_id] => 4
[problem_update_date] => 2010-10-12
[problem_update_text] => An update about the problem with an ID of four
[problem_update_active] => 1
)
[3] => Array
(
[problem_update_id] => 6
[problem_id] => 4
[problem_update_date] => 2010-10-12
[problem_update_text] => An update about the problem with an ID of 6
[problem_update_active] => 1
)
[4] => Array
(
[problem_update_id] => 7
[problem_id] => 3
[problem_update_date] => 2010-10-12
[problem_update_text] => Some new update about the problem with the ID of 3
[problem_update_active] => 1
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想要 id 3 的问题的 Problem_update 列表,为什么不限制您的(我的)sql 语句仅返回这些? 无论如何,
WHERE Problem.problem_id = 3
:
If you just want a list of problem_update for the problem with the id 3 why dont you limit your (my)sql-statemant to retunr only these?
WHERE problem.problem_id = 3
anyway:
对不起@maggie。这与你写的几乎相同,但在功能上。希望这会对您有所帮助。当然,如果我理解正确的话。
Sorry @maggie. This is almost same as what you wrote, but in function. Hope that will help you. Of course if I understod you correctly.
玛吉是对的:你不应该通过迭代数组来查找键。只需在 SQL 中添加
project_id = 3
作为条件即可。使用
get_where()
:或
where()
:有关详细信息,请查看出色的 Codeigniter 文档:
Maggie is right: you shouldn't look for the key by iterating over the array. Just add
project_id = 3
as a condition in SQL.Either use
get_where()
:Or
where()
:For more info check the great Codeigniter documentation: