0

I have this code which returns an error:

$response = curl_exec($client);

$result = json_decode($response);

$output = '';

if(count($result) > 0)

{

foreach($result as $row)

{

$output .= '

<tr>

<td>'.$row->name.'</td>

<td>'.$row->url.'</td>

Error

Parameter must be an array or an object that implements Countable?

Emma
  • 27,428
  • 11
  • 44
  • 69
Sinead
  • 1
  • 5

1 Answers1

0

Your code has some synatx errors first, which you might solve it before that. I'm guessing that you $result might be an array. Then, you can add is_array in your if to check that for you.

$response = curl_exec($client);
$result = json_decode($response, true);
$output = '';

if (count($result) > 0 && is_array($result)) {
    foreach ($result as $row) {
        $output .= '<tr><td>' . $row->name . '</td><td>' . $row->url . '</td></tr>';
    }
}else{
    die("Sorry! Result is not an array!");
}

This is also a common error. You might look into some similar posts such as this post which explains about that error.

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    Could you explain your improvements to OP's code, Emma? How did you verify that the code no longer throws the same error as in the question? – Dharman May 03 '19 at 21:40