views:

63

answers:

3

Hi all,

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
  )
)
+1  A: 

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'];
    }
}
maggie
+1 for suggesting using sql for finding records with `project_id = 3`
captaintokyo
Just to clarify, because this is something I should have mentioned now that I've read your responses, the purpose of the view is to display all problems, and all the updates associated with that problem. Therefore, the reason why I didn't use WHERE to narrow down my SQL result is because the controller passes all the updates and all the problems to the view. A foreach loop then runs through all the problems and ouputs them in a user friendly format so that all problems are displayed on the one page. Thus the purpose of going through the array to find the update with the appropriate problem id.
Jeremy
thanks for this Maggie :) I've just accepted Eugene's response as it is in a function.
Jeremy
perhaps then it would be better to write a method in your model (problem) which return an associated array (problem -> problem_update). why should the view associate data? it should simply display it. but i guess you know that (if it where cakePHP or doctrine i would provide sample-code ;) )
maggie
A: 

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 );
Eugene
Thanks so much for this. I really appreciate it. Will test shortly. I broke the MVC pattern as a temporary solution to my problem, but, this is the way I'd rather attack it.
Jeremy
A: 

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:

captaintokyo
Just to clarify, because this is something I should have mentioned now that I've read your responses, the purpose of the view is to display all problems, and all the updates associated with that problem. Therefore, the reason why I didn't use WHERE to narrow down my SQL result is because the controller passes all the updates and all the problems to the view. A foreach loop then runs through all the problems and ouputs them in a user friendly format so that all problems are displayed on the one page. Thus the purpose of going through the array to find the update with the appropriate problem id.
Jeremy
If you are going to display all problems and all updates there is no need to *search* in an array for `problem_id = 3`. IMO, a better approach would be to iterate over all problems and use sql to find the updates associated with that problem and iterate over them.
captaintokyo