简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM