Errorexception : array_push() expects Parameter 1 to be array, string Given

Asked

Viewed 239 times

1

I have a problem with this function that generates a latitude and longitude Polyline. However whenever I execute the Laravel’s Seed it generates the following error: php ErrorException : array_push() expects parameter 1 to be array,string given

The function I call within Seed is as follows:

    public function gerarPolyline()
    {
        $fake = \Faker\Factory::create('pt_BR');
        $polyline = ['latitude' =>'' , 'longitude' => '' ];

       for($i =0; $i < 1000; $i++){

            $latitude = $fake->latitude($min = -90, $max = 90);
            $longitude = $fake->longitude($min = -180, $max = 180);
            array_push($polyline['latitude'], $latitude);
            array_push($polyline['longitude'],$longitude);
        }
        return $polyline;
    }

1 answer

1


The mistake happens because the array_push() is waiting for two arguments. Change your code like this:

public function gerarPolyline()
    {
        $fake = \Faker\Factory::create('pt_BR');
        $polyline = ['latitude' =>'' , 'longitude' => '' ];

       for($i =0; $i < 1000; $i++){

            $latitude = $fake->latitude($min = -90, $max = 90);
            $longitude = $fake->longitude($min = -180, $max = 180);
            array_push($this->$polyline['latitude'], $latitude);
            array_push($this->$polyline['longitude'],$longitude);
        }
        return $polyline;
    }
  • 3

    Really that’s what happened, ErrorException : Array to string conversion, and giving dd($latitude), it is returned to me something like: 42.878878 or a random latitude, I do not understand why is not performing the push.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.