在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
&GT;
答案 0 :(得分:0)
你试过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.
?>
感谢。
答案 1 :(得分:0)
您要找的是foreach($array as $key => $val){
if($key == 'product_image'){
echo "<img src='".$val."' />";
}
}
。
结帐以下链接,了解如何操作。
https://www.sitepoint.com/php-streaming-output-buffering-explained/