skicrud Posted April 9, 2009 Share Posted April 9, 2009 I have created the following function: function get_prepress_checked_items($job_number){ $checked_items=array(); $sql='SELECT * from rs_prepress_log where job_number='.sprintf('%d',$job_number); $query=$this->db->query($sql); $query->result_array(); //$rtn=$query->result(); #returns the results from the log file // $checked_items=array(); foreach($query->result_array() as $row) { $checked_items[]=$row['item_checked']; $checked_items[]=$row['item_comment']; } return $checked_items; } when I execute the following code echo '<pre>';var_dump($checked_items);echo '</pre>'; I get back: array(4) { [0]=> string(3) "A00" [1]=> string(9) "comment 1" [2]=> string(3) "A20" [3]=> string(9) "comment 2" } My question is how do I set up a loop to get the "comment 1" and "comment 2" variables? Quote Link to comment https://forums.phpfreaks.com/topic/153329-access-values-in-an-array/ Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 Are the array elements always going to be in the same order/position? Quote Link to comment https://forums.phpfreaks.com/topic/153329-access-values-in-an-array/#findComment-805544 Share on other sites More sharing options...
skicrud Posted April 9, 2009 Author Share Posted April 9, 2009 Yes, however there might be more or less depending on how many entries are in the db. There might be times when there are no comments but then the value will be null and that is ok. Quote Link to comment https://forums.phpfreaks.com/topic/153329-access-values-in-an-array/#findComment-805554 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 OK, is it always going to be the comment is every other element? Quote Link to comment https://forums.phpfreaks.com/topic/153329-access-values-in-an-array/#findComment-805562 Share on other sites More sharing options...
skicrud Posted April 9, 2009 Author Share Posted April 9, 2009 Yes Quote Link to comment https://forums.phpfreaks.com/topic/153329-access-values-in-an-array/#findComment-805567 Share on other sites More sharing options...
Maq Posted April 9, 2009 Share Posted April 9, 2009 So if every other element in the array is going to be a comment then you just grab every other element. Or, in your case, it looks like all the ODD indexed elements. You can probably achieve this by using the modulus operator. Quote Link to comment https://forums.phpfreaks.com/topic/153329-access-values-in-an-array/#findComment-805581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.