I have function like this:
function ypg_delete_img($id, $img)
{
$q = $this->ypg_get_one($id);
$imgs = explode(',', $q->image);
if(count($imgs) > 1) :
$z = ",";
else :
$z = "";
$data['image'] = 'avatar_mali_oglas.png';
$this->db->where('id_yellow_pages', $id);
$this->db->update('yellow_pages', $data);
endif;
if($imgs[0] != 'avatar_mali_oglas.png') :
$query = "UPDATE `yellow_pages` ";
$query .= "SET `image` = REPLACE(`image`,'". $img . $z ."', '')
WHERE `id_yellow_pages` = $id ";
$this->global_functions->delete_img('zute_strane', $img);
$this->db->query($query);
endif;
}
I need to check if $img is the last value in the array of $imgs. How can I do this?
Use end()
if ($img == end($imgs)) {
// $img is the last element of the array
}
Please refer below code.
if($imgs[count($imgs) -1] == $img)
{
// $img is the last image
}
Hope this will be helpful :)
Check end() : http://php.net/manual/fr/function.end.php
Like : if($img == end($imgs))
Something like this should work:
<?php
$lastitem = array_pop($imgs);
if ($img == $lastitem) {
// is last image
}
?>
array_pop returns the last element of an array, after that you just need to compare the two values.