I have written this code with Yii2 a long time ago to fetch photos for a slideshow and then choose another image size among several images and although it works, as I haven't been working with PHP recently, I don't understand why. I think I should be able to use a simple foreach loop, but when I change it to a simple one, it doesn't work. This is the code:
public static function getSlideShowPage($type)
{
$slideShow=SlideShow::find()
->select(['title','image','title_position','subtitle', 'title_color'])
->where(['deleted' => 0,'active'=>1,'type'=>$type])
->orderBy('id ASC')
->groupBy('id')
->asArray()
->all();
if(!empty($slideShow))
foreach ($slideShow as $key=>$slide){
$slideShow[$key]['image']=str_replace('_xs', "", $slide['image']);
}
return $slideShow;
}
and what I'm having a problem with is this part:
if(!empty($slideShow))
foreach ($slideShow as $key=>$slide){
$slideShow[$key]['image']=str_replace('_xs', "", $slide['image']);
}
Why can't I write it like this:
if(!empty($slideShow))
foreach ($slideShow as $slide){
str_replace('_xs', "", $slide['image']);
}
In each foreach loop, I choose one of the array elements and replace the URL of the image. I would appreciate any help or suggestion.