为foreach()php警告提供的参数无效

Iam newbie to php and codeigniter.

I have error in php,

 A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/search.php

Line Number: 44

Line 44 is: foreach ( $data as $key => $value )

There is some syntax error here, inside this if statement, if (!empty($user['search'])), $data is printed correctly,whereas in this if statement,if(!empty($data)), $data is not printed. Can anyone help me resolve this issue.

The problem is you don't have any value inside your looping variable( $data ).

You can print their values for check whether the variable contains values.

Replace :

$data = json_decode($user['search'])->result;

By :

$data = json_decode($user['search'], true);

json_decode works that way :

var_dump(json_decode($json)); // returns object(stdClass)
var_dump(json_decode($json, true)); returns array

Your $data was an object, while foreach strictly wants an array.