Skip to content
Advertisement

How to display the first image name from a string of image names

This is my array. I want to print the first image name from the key [image_names]

[prop] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [property_type_id] => 1
                    [property_category_id] => 2
                    [user_id] => 1
                    [city_id] => 1
                    [title] => House for sale
                    [description] => 
Flat for sale


                    [image_names] => bed48d08-5dfc-438e-babd-51a265e937a9.jpg,e2c2769e-ac86-4a77-b1cb-3cb07c483b2a.jpg
                    [size] => 1000
                    [additional_features] => yards
                    [price] => 1200000
                    [address] => ffdfdsfs
                    [youtube] => NULL
                    [referance] => 1233567
                    [currency] => Rs.
                    [bed_room_count] => 0
                    [bath_room_count] => 0
                    [kitchen_room_count] => 0
                    [parking_count] => 0
                    [latitude] => 0
                    [longitude] => 0
                    [created_at] => 2020-11-29 06:33:43
                    [updated_at] => 2020-12-06 04:39:28
                    [featured] => 1
                    [visible] => 1
                )

            [1] => Array
                (
                    [id] => 2
                    [property_type_id] => 1
                    [property_category_id] => 2
                    [user_id] => 1
                    [city_id] => 1
                    [title] => Flat for sale
                    [description] => 
Flat for sale Flat for sale


                    [image_names] => 9eef3b07-b8f2-423b-9775-5339eba48947.jpg,7d28182b-4af0-4a72-b670-61a46bf4efd7.jpg
                    [size] => 1200
                    [additional_features] => yards
                    [price] => 13221
                    [address] => 31313311 fgdfgdgdggdf
                    [youtube] => NULL
                    [referance] => 111121
                    [currency] => Rs.
                    [bed_room_count] => 0
                    [bath_room_count] => 0
                    [kitchen_room_count] => 0
                    [parking_count] => 0
                    [latitude] => 0
                    [longitude] => 0
                    [created_at] => 2020-12-06 04:38:12
                    [updated_at] => 2020-12-06 04:44:33
                    [featured] => 1
                    [visible] => 1
                )

        )

I tried the following code but its not working

@foreach ($prop as $pc1)
                
<?php 
echo explode("', '",'"'.$pc1['image_names'].'"');
?>
@endforeach 

Advertisement

Answer

  • There’s no space in the seperator
  • No need to quote the search string
  • Since explode(), returns an array you’ll need the first entry to get the image string [0]

Please take a close look at explode docs

@foreach ($prop as $pc1)
                
<?php 
echo explode(',', $pc1['image_names'])[0];
?>
@endforeach 
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement