在多维数组中搜索值,然后输出数组中的其他值

发布于 2024-09-27 01:33:58 字数 1858 浏览 1 评论 0原文

我想知道如何在下面的数组中搜索关键 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉墨 2024-10-04 01:33:58

如果您只想要 id 3 的问题的 Problem_update 列表,为什么不限制您的(我的)sql 语句仅返回这些? 无论如何, 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'];
    }
}

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:

foreach( $updates->result_array() as $update ) {
    if( 3 == $update['problem_id'] ) {
        echo $update['problem_update_date'] . ' : ' . $update['problem_update_text'];
    }
}
千笙结 2024-10-04 01:33:58

对不起@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 );

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.

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 );
冷夜 2024-10-04 01:33:58

玛吉是对的:你不应该通过迭代数组来查找键。只需在 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 文档:

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():

$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;
}

Or 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;
}

For more info check the great Codeigniter documentation:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文