【问题标题】:Display foreach array item one by one in ajax在ajax中一一显示foreach数组项
【发布时间】:2016-10-07 07:31:11
【问题描述】:

在 PHP 中,我从 foreach 获取一个数组。我是 JSON 编码并一次显示所有结果。这工作正常。但是如果我需要在 foreach 仍然继续的情况下一个一个地显示数组项怎么办?

<?php 
$array = //somearray;
$data_arr = array();
foreach($array as $arr){
//do something
$data_arr[] = $arr;
}
echo json_encode(array('success'=>true,'data'=>$data_arr));
//here i can display the data in my jquery using each function
//i can display the whole data at once after the foreach is completed
//what i need is i want to display first element of data_arr after its loop is completed and then the second element and so on

?>

【问题讨论】:

标签: php ajax


【解决方案1】:

你试过http://www.php.net/array_shift 吗?它的php函数你需要在foreach之后调用这个函数

<?php 
foreach($array as $arr){
//do something
$data_arr[] = $arr;
}

print_r array_shift($data_arr);
//Print First array value from this $data_arr list.

?>

谢谢。

【讨论】:

    【解决方案2】:

    你要找的是Streaming Response

    查看以下链接以了解如何操作。

    https://www.sitepoint.com/php-streaming-output-buffering-explained/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2011-09-10
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多