Jump to content

Searching multidimensional for values, then outputting other values in array


jeremydt

Recommended Posts

Hi all,

 

I'm a first time poster here and I would really appreciate some guidance with my latest php challenge! I've spent the entire day googling and reading and to be honest I think I'm really over my head and need the assistance of someone experienced to advise the best way to go!

 

I have a multi dimensional array that looks like (see below); the array is created by CodeIgniter's database library (the rows returned from a select query) but I think this is a generic PHP question as opposed to having anything to do with CI because it related to working with arrays.

 

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, as above, 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
        )

)

 

foreach($updates->result_array() as $array) {
   if($array['problem_id'] == 3) {
      echo $array['problem_update_date'] . ' : ' . $array['problem_update_text'];
   }
}

Simply use a WHERE clause in your SELECT query to return only the rows you are interested in -

 

$some_variable = 3;

$query = "SELECT * FROM your_table WHERE problem_id = $some_variable";

Simply use a WHERE clause in your SELECT query to return only the rows you are interested in -

 

$some_variable = 3;

$query = "SELECT * FROM your_table WHERE problem_id = $some_variable";

:o DOH!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.