2017-08-26 246 views
-1

我使用從Bittrex的API,讀取JSON,然後用一個循環通過每個迴路得到的信息,我需要PHP for each循環

一切正常返回只有某些元素。這裏是代碼:

 foreach($return[result] as $x=>$x_value){ 
      extract($x_value); 
      echo $OrderUuid." " .$closedDate ." " .$closedTime ." " .$Quantity." " .$Price." " . $PricePerUnit * 100000000 ." <br>"; 
     } 

不過,我想這樣做,因爲我把它放到表分頁是我需要能夠怎麼只能隨聲附和出像1-5再下一個頁面的結果將是6-11等等

我知道計數已經通過使用它,它返回的結果量。

$thisManyResults=count($return[result]); 

我知道我需要記住,索引也是從零開始的。

循環讓我感到非常困惑,請給出一個解釋,讓新手環繞他的頭。謝謝

編輯:

foreach($return[result] as $x=>$x_value){ 
    extract($x_value); 


    $str66 = str_replace('T', ' ', $TimeStamp); 
    $newarray = explode(" ", $str66); 
    $date =date_create($newarray[0]); 
    $closedDate = date_format($date,"m/d/Y"); 
    $closedTime = date("g:i:s a", strtotime($newarray[1])); 


    // echo $OrderUuid."  " .$closedDate ."   " .$closedTime ." " .$Quantity."  " .$Price."  " . $PricePerUnit * 100000000 ." <br>"; 


     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'>". $Quantity ." </td>"; 
     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'><center>". $Price." </center></td>"; 
     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'><center>". $PricePerUnit * 100000000 ." </center></td>"; 
     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'><center>". $closedDate." </center></td>"; 
     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'><center>". $closedTime." </center></td></tr>"; 

    } 

但這顯示所有結果0-22(23結果)我需要能夠僅在表0顯示直通5或6直通10

回答

0

這裏,以防別人最後的代碼可以幫助

$start="0"; 
$end ="8"; 
foreach (array_slice($return[result], $start ,$end) as $x=>$x_value) { 
    extract($x_value); 

    $str66 = str_replace('T', ' ', $TimeStamp); 
    $newarray = explode(" ", $str66); 
    $date =date_create($newarray[0]); 
    $closedDate = date_format($date,"m/d/Y"); 
    $closedTime = date("g:i:s a", strtotime($newarray[1])); 

     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'>". $Quantity ." </td>"; 
     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'><center>". $Price." </center></td>"; 
     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'><center>". $PricePerUnit * 100000000 ." </center></td>"; 
     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'><center>". $closedDate." </center></td>"; 
     echo "<td style='text-decoration:none;color:#000000;font-weight:normal'><center>". $closedTime." </center></td></tr>"; 

    } 

} 

我通過@ gabriele的回答得到了這個信息,他發佈了大約array_slice

1

希望這可以幫助你

// Array example 
$array=[ 
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; 
$_GET["page"] = 1; // This is variable that you have to pass 
$pagination = 4; // Number of items 4 = 5, 5 = 6 
if(!isset($_GET["page"])){ 
    // If $_GET is not null, starting page from 0 to 4 (total 5 items) 
    $start = 0; 
    $end = $pagination; 
}else{ 
    //else, calculate variable pagination * number page 
    $start += $pagination * ($_GET["page"] - 1); 
    //increments end variable 
    $end += $start + $pagination; 
} 
// Range from array 
$range=[$start,$end]; 
$a=array_keys(array_intersect(array_keys($array),$range)); 
// slice the desired elements using offset and calculated length 
foreach( array_slice($array,$a[0],$a[1]-$a[0]+1) as $v){ 
    // Printing results 
    echo $v . " "; 
} 
+0

我實際上已經將分頁算出來了。我只需要能夠僅從結果#5到#10顯示(回顯)... api沒有開始和結束,所以我必須每次都調用它。 –

+0

即時嘗試添加數組切片現在更新的代碼上面 –

+0

我的代碼只是一個例子,你不需要從API開始和結束,如果你有所有的結果已經存儲在數組中 –