0

I have the following function.

public function searchAirports($query)
    {
            $url = 'storage/json/airports.json';
            $file = file_get_contents($url);
            $data = json_decode($file, true);

            $data = array_filter($data);
            $needles = ['name','city','country'];
            $NeedledResults = [];
            for( $i=0; $i < $needles; $i++ ) {
                $param = $needles[$i];
                $results= collect($data)->filter(function ($item) use ($query){ return false !== stristr($item[$param], $query); });
                $a = $results->toArray();
                array_push($NeedledResults,$a);
            }

            dd($NeedledResults);


           return $filteredResults;
    }

Basically I have three filters in which to serach a json data set by, name, city and country. this function might not be beautiful, but it does with if I replace the $param variable with one of the needles strings directly.

for example stristr($item['city'], $query); works perfectly, but if I replace this with $param it errors as undefined.

This is driving me nuts! if anyone can point me in the right direction it would be much appriciated.

Thanks all :)

  • A very confused, albeit determined jr developer.
Lewis Clarke
  • 69
  • 2
  • 12

0 Answers0