PHP ob_end_clean不清除缓冲区

What I'm trying to do is to response with a JSON array of output of PHP files.

header("Content-Type: application/json");
$ret["response"] = array();

$items = getItems();
$ret["response"] = array();
ob_start();
foreach($items as $model){
    include("view/item.php");
    $ret["response"][] = ob_get_contents();
    ob_end_clean();
}
ob_end_clean();
echo json_encode($ret);

the file view/item.php contains some PHP echo statements, but the problem is that response contains the output of include statement and the JSON encoded items.

how do i make the script return only the JSON part?

Try your code like,

foreach($items as $model){
    ob_start();
    include("view/item.php");
    $ret["response"][] = ob_get_contents();
    ob_end_clean();
}
ob_end_clean();

or,

ob_start();
foreach($items as $model){
    include("view/item.php");
    $ret["response"][] = ob_get_contents();
}
ob_end_clean();

Second option will work fine.

ob_start();
foreach($items as $model){
  include("view/item.php");
  $ret["response"][] = ob_get_contents();
}
ob_end_clean();