0

I have this problem: I read xml with PHP in Laravel, if I have only one result it doesn't print me the zero key. This creates a problem for me when I foreach the data.

$xml = Http::get(URL);
$array = XmlToArray::convert($xml);
$results = $array['farmaci'];
dd($results);

@foreach ($results as $result)
$result['name'];
@endforeach 

Undefined array key "name", in case I have only one result

I would like that if I have only one result it also prints the zero key of the element therefore:

  • `if (!array_is_list($array)) { $array = [$array]; }` ...then your `foreach()` will consistently receive an array where the first level will be indexed. – mickmackusa Mar 10 '23 at 02:14

0 Answers0