2016-10-07 9 views
3

PHPでは、foreachから配列を取得しています。 JSONをエンコードしてすべての結果を一度に表示しています。これは正常に動作します。しかし、foreachがまだ続行しているので、配列アイテムを1つずつ表示する必要がある場合はどうすればよいですか?foreach配列アイテムを1つ1つajaxで表示する

<?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 

?>

+0

あなたは –

+0

あなたはjqueryの応答でそれぞれを使用することができます持っているコードを提供してください。 –

+0

foreachの内側にループするforeachを追加します。 –

答えて

0

あなたはhttp://www.php.net/array_shiftを試してみましたか?あなたのforeachの後にこの関数を呼び出す必要があるphp関数

<?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. 

?> 

ありがとう。

関連する問題