我想知道如何在下面的数组中搜索关键的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
)
)
答案 0 :(得分:2)
如果你只想找到一个id_update列表来解决id 3的问题,为什么不限制你的(我的)sql-statemant只重新调整这些? WHERE problem.problem_id = 3
反正:
foreach( $updates->result_array() as $update ) {
if( 3 == $update['problem_id'] ) {
echo $update['problem_update_date'] . ' : ' . $update['problem_update_text'];
}
}
答案 1 :(得分:1)
抱歉@maggie。这几乎和你写的一样,但在功能上。希望能帮到你。当然,如果我正确地理解你的话。
function youAskedForIt( $some_array = array(), $value_to_search_for ) {
foreach( $some_array as $value ) {
if( ! isset( $value['problem_id'] ) || ! isset( $value['problem_update_date'] ) || ! isset( $value['problem_update_text'] ) ) {
continue;
}
if( $value_to_search_for == $value['problem_id'] ) {
echo $value['problem_update_date'] . ' => ' . $value['problem_update_text'] . "<br>\n";
}
}
}
youAskedForIt( $updates->result_array(), 3 );
答案 2 :(得分:0)
Maggie是对的:你不应该通过迭代数组来寻找密钥。只需在SQL中添加project_id = 3
作为条件。
使用get_where()
:
$query = $this->db->get_where('my_table_name', array('problem_id' => 3));
foreach($query->result() as $row)
{
echo $row->problem_update_date;
echo $row->problem_update_text;
}
或where()
:
$this->db->where('project_id', 3);
$query = $this->db->get('my_table_name');
foreach($query->result() as $row)
{
echo $row->problem_update_date;
echo $row->problem_update_text;
}
有关更多信息,请查看优秀的Codeigniter文档: