0

In Database :- "["http://localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg","http://localhost//image/959776b99b006e5785c3a3364949ce47.jpg"]"

After Json_decode = ["http://localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg","http://localhost//image/959776b99b006e5785c3a3364949ce47.jpg"]

Output i Want:- [0] - http://localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg [1] - http://localhost//image/959776b99b006e5785c3a3364949ce47.jpg

Code:-

   @if(is_array(json_decode($data->item_image)))
                                    @php $image = json_decode($data->item_image);
                                    @endphp
                                            @foreach($image as $key=>$row)
                                                @if($key == 0)
                                                <div class='carousel-item active'>
                                                    <!-- <img class='img-size img-responsive' src="{{ asset('image/'. $row) }}" /> -->
                                                    <img class='img-size img-responsive' src="{{ $row }}" />
                                                </div>
                                                @else
                                                <div class='carousel-item'>
                                                    <!-- <img class='img-size' src="{{ asset('image/'. $row) }}" /> -->
                                                    <img class='img-size' src="{{ $row }}" />
                                                </div>
                                                @endif
                                            @endforeach
                                    @else

                                    @endif
6
  • are you getting an array of strings as json response ? Commented Feb 1, 2022 at 5:45
  • Yes after json_decode i'm getting a string Commented Feb 1, 2022 at 6:15
  • I'm kinda confused, looks like you're already getting an array of Strings? You could break apart the string by doing stringName.split(","). For ex: String longString = "zyzabcstring.jpg,string2.jpg,string3.jpg"; String[] strings = longString.split(","); Commented Feb 1, 2022 at 6:30
  • use explode function Commented Feb 1, 2022 at 6:36
  • add your code, so we can see exactly what you are doing. Commented Feb 1, 2022 at 6:44

2 Answers 2

2

you can use this

$str = '["http://localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg",
"http://localhost//image/959776b99b006e5785c3a3364949ce47.jpg"]';
eval("\$myarray = $str;");
print_r($myarray);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You Sir Now it's Working Thanks a lot... !!
Why with eval()? eval should never be used if there is another solution.
1
 @if(json_decode($data->item_image) != '')
                                    @php $image = json_decode($data->item_image);
                                            eval("\$myarray = $image;");
                                    @endphp
                                            @foreach($myarray as $key=>$row)
                                                @if($key == 0)
                                                <div class='carousel-item active'>
                                                    <!-- <img class='img-size img-responsive' src="{{ asset('image/'. $row) }}" /> -->
                                                    <img class='img-size img-responsive' src="{{ $row }}" />
                                                </div>
                                                @else
                                                <div class='carousel-item'>
                                                    <!-- <img class='img-size' src="{{ asset('image/'. $row) }}" /> -->
                                                    <img class='img-size' src="{{ $row }}" />
                                                </div>
                                                @endif
                                            @endforeach
                                    @else

                                    @endif

7 Comments

99,99% of the time eval() is not needed to solve a problem. In this case it is not needed, so don't use it.
Can you suggest an alternative for eval() in this case ?
Json decode comes with a second parameter "associative", so json_decode($data->item_image, true) might give the array. If that doen't work, you can explode the string. I personally never needed to use eval() because there was always another (so better) way.
@GertB After Exploding it'll give output like Array = ( [0] => ["localhost//image/aeefb050911334869a7a5d9e4d0e1689.jpg" [1] => "localhost//image/959776b99b006e5785c3a3364949ce47.jpg"] )But this is Not i want
Well, what if I would add php code in the database column, eval() will run it. thats a big security risk.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.