ErrorException : Array to string conversion in PHP

44,695

Solution 1

use the following code

{!! str_replace("'", "\'", json_encode($sliderImageDataArray)) !!};

instead of

$sliderImageDataArray = implode(" ",$sliderImageDataArray);

Solution 2

Notice the [] after $sliderImageDataArray. You're actually assigning the array with those values to the first element of the array that is $sliderImageDataArray. So, implode() actually tries to join by that array, not the one inside. And since the resulting pieces are an array, not a string, it gives you the Array to string conversion error. Remove the []:

<?php
$sliderImageDataArray =array(
                "title"=> "test title",
                "text"=> "<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">value<\/span><\/p>\n<p><a class=\"btn  btn-secondary\" href=\"http://localhost/sencare/book-appoinment/\" target=\"_self\">  Make An Appointment <\/a>\u00a0<a class=\"btn  btn-light\" href=\"http://localhost/sencare/our-doctors/\" target=\"_self\"> Our Doctors<\/a><\/p>\n",
                "is_video"=> false,
            );

$sliderImageDataArray = implode(" ",$sliderImageDataArray);

echo $sliderImageDataArray;

And it works correctly.

Demo

Share:
44,695

Related videos on Youtube

raff
Author by

raff

Updated on July 09, 2022

Comments

  • raff
    raff almost 2 years

    I want to convert an array to a string in Laravel. I have already searched and implement the implode() function to convert this but got this error

    ErrorException (E_NOTICE) Array to string conversion
    

    Here is my code in controller

    $sliderImageDataArray[] =array(
                    "title"=> $value->title,
                    "text"=> "<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">".$value->text."<\/span><\/p>\n<p><a class=\"btn  btn-secondary\" href=\"http://localhost/sencare/book-appoinment/\" target=\"_self\">  Make An Appointment <\/a>\u00a0<a class=\"btn  btn-light\" href=\"http://localhost/sencare/our-doctors/\" target=\"_self\"> Our Doctors<\/a><\/p>\n",
                    "is_video"=> false,
                );
    
    $sliderImageDataArray = implode(" ",$sliderImageDataArray);
    
    return $sliderImageDataArray;
    

    Anybody help please

    • ishegg
      ishegg over 6 years
      We can’t help you if you post no code.
    • raff
      raff over 6 years
      @ishegg..... I have update my post. please check
  • raff
    raff over 6 years
    I have a bunch of data getting from database and need to keep them in an array format and send them to the view. That's why I used [] in sliderImageDataArray.
  • ishegg
    ishegg over 6 years
    I see, but implode() can't join arrays. You'll need to loop $sliderImageDataArray with foreach() and implode each element individually.
  • raff
    raff over 6 years
    @ishegg.....please see this post [stackoverflow.com/questions/48970613/…
  • raff
    raff over 6 years
    Ohh sorry my friend for not giving the answer here yet...yes ... Here is the answer....{!! str_replace("'", "\'", json_encode($sliderImageDataArray)) !!};