CodeIgniter Forums

Full Version: search in result_array()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi...
im returning db query as result array:

$sql="select image_name,image_caption from gallery_image_captions where gallery_id=".$gallery_id." order by image_name";
$query = $this->db->query($sql);
return $query->result_array();

now i have
image_name1 "image caption 1"
image_name2 "image caption 2"
image_name3 "image caption 3"
image_name4 "image caption 4"

how can i found in the result array a value by some key? i mean the function will get key (image_name3) and value will be "image caption 3"

thanks
PHP Code:
$result['image_name3'

Check out docs for result_array
im getting undefined index...
i believe the data is:
this gived me "array"
echo $captions_arr[0];

and this gives me error...
$captions_arr[0][0];

result_array is not an array of arrays?...
Ah right, it's multiarray so you need to iterate through it like:
PHP Code:
foreach ($query->result_array() as $row)
{
 
   if (isset($row['image_name3'])) echo $row['image_name3'];


I really think you should put this into your query condition instead if you want only one row, though.
now i got it...

return $captions_arr[0]["text_heb"];

so - basically i want to print the value of "text_heb" only when image_name is equal to specific value...

$array = array(
0 => array(
'image_name' => 'img_65.jpg',
'text_heb' => 'my text 1'
),
1 => array(
'image_name' => 'img_66.jpg',
'text_heb' => 'my text 2'
),
thanks for help...

public function get_caption($captions_arr,$file_name)
{
foreach ($captions_arr as $row)
{
if($row["image_name"] == $file_name)
{
return $row["text_heb"];
}
}
}