【问题标题】:Searching multidimensional array for values, then outputting other values in array在多维数组中搜索值,然后在数组中输出其他值
【发布时间】:2010-10-12 08:15:42
【问题描述】:

我想知道如何在下面的数组中搜索关键问题 ID 和等于我将提供的变量的值。然后,当它找到具有匹配键和变量的数组时,它也会输出该数组部分中的其他值。

例如,使用下面的示例数据。您如何建议我在数组中搜索所有具有键 issue_id 和值 3 的数组,然后让它输出键 issue_update_date 的值和键 issue_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
  )
)

【问题讨论】:

    标签: php arrays codeigniter multidimensional-array


    【解决方案1】:

    如果您只想要 id 3 问题的问题更新列表,为什么不将您的 (my)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 建议使用 sql 查找具有 project_id = 3 的记录
    • 澄清一下,因为这是我现在应该提到的,因为我已经阅读了您的回复,视图的目的是显示所有问题以及与该问题相关的所有更新。因此,我之所以没有使用 WHERE 来缩小 SQL 结果的范围,是因为控制器将所有更新和所有问题都传递给了视图。然后 foreach 循环遍历所有问题并以用户友好的格式输出它们,以便所有问题都显示在一个页面上。因此,通过数组查找具有适当问题 id 的更新的目的。
    • 感谢这个 Maggie :) 我刚刚接受了 Eugene 在函数中的回复。
    • 也许最好在模型(问题)中编写一个返回关联数组(问题->问题更新)的方法。为什么视图要关联数据?它应该简单地显示它。但我想你知道(如果它在 cakePHP 或学说中我会提供示例代码;))
    【解决方案2】:

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

    【讨论】:

    • 非常感谢。对此,我真的非常感激。不久将进行测试。我打破了 MVC 模式作为我的问题的临时解决方案,但是,这是我宁愿攻击它的方式。
    【解决方案3】:

    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 文档:

    【讨论】:

    • 澄清一下,因为这是我现在应该提到的,因为我已经阅读了您的回复,视图的目的是显示所有问题以及与该问题相关的所有更新。因此,我之所以没有使用 WHERE 来缩小 SQL 结果的范围,是因为控制器将所有更新和所有问题都传递给了视图。然后 foreach 循环遍历所有问题并以用户友好的格式输出它们,以便所有问题都显示在一个页面上。因此,通过数组查找具有适当问题 id 的更新的目的。
    • 如果您要显示所有问题和所有更新,则无需在problem_id = 3 的数组中搜索。 IMO,更好的方法是遍历所有问题并使用 sql 查找与该问题相关的更新并遍历它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2011-05-20
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多