PHP - 为foreach()警告消息提供的参数无效

时间:2016-08-18 05:48:49

标签: php serialization foreach

$data = file_get_contents($file);

$fexplode = explode(PHP_EOL, $data);

foreach ($fexplode as $uline) {
     foreach (unserialize($uline) as $item => $value){ 
         echo $item . " :  ";
         echo $value;
     }
} 

运行此代码时,在我的页面上出现“警告:为foreach()提供的参数无效”警告

1 个答案:

答案 0 :(得分:0)

以下方式使用

$data = file_get_contents($file);

$fexplode = (!empty($data)?explode(PHP_EOL, $data):array()); // use !empty otherwise make empty array

foreach ($fexplode as $uline) {
     foreach (unserialize($uline) as $item => $value){ 
         echo $item . " :  ";
         echo $value;
     }
}