簡體   English   中英

無法弄清楚如何制作此PHP Array表

[英]Can't figure out how to make this PHP Array table

我似乎無法弄清楚如何打印一個漂亮的有序表。

我希望最多有7列,並且它可以根據數組的大小生成所需數量的行。 該數組是通過可隨時更新的URL進行的。 (這是玩家的庫存清單)。

$id = $steamprofile['steamid'];
$key = 'XXXXXXXXXXXXXXXXXXXX';
if($id != null){
  $inv = file_get_contents('http://steamcommunity.com/profiles/'.$id.'/inventory/json/730/2');
  $inventory = json_decode($inv, true);

  $price = file_get_contents('values/response.json');
  $value = json_decode($price, true);

  foreach ($inventory['rgDescriptions'] as $rgDescription) {                                
    for($i = 0; sizeof($rgDescription['market_name']) > $i; $i++){
      if(isset($rgDescription['market_name'])){
        print '<td><img src="https://steamcommunity-a.akamaihd.net/economy/image/'.$rgDescription['icon_url'].'" alt="'.$rgDescription['market_name'].' width="80" height="75"></td>';
      }
    }
  }
}

如果需要查看該陣列,則位於此處

我可以打印出表格,但它總是重復我不想要的項目。 那么我該如何解決呢? 任何建議深表感謝。

您只需要計算列數:

$max = 7;
$col = 1;
for(...) {
   if ($col == 1) {
      echo '<tr>'; // start new row if on column #1
   }
   echo '<td><img etc....'; // output a column
   if ($col == $max) {
     echo '</tr>'; // if column $max was just output, end the row
     $col = 0; // reset column count, 0 to account for ++ coming up next
   }
   $col++;
}

"market_name"不是一個數組,而是一個元素。 如果您countcount (與sizeof相同),它將返回1因為它僅分配了一個值。 就是說,您的for($i = 0; sizeof($rgDescription['market_name']) > $i; $i++)的行與for($i = 0; 1 > $i; $i++) ,返回總是一樣的結果。

foreach扮演重復部分,然后您將獲得一張包含相同行的巨大桌子。

意見建議:

foreach ($inventory['rgDescriptions'] as $rgDescription) {                                
    foreach ($rgDescription as $rg) {
        if(isset($rg['market_name'])) {
            print('
                <td>
                    <img src="https://steamcommunity-a.akamaihd.net/economy/image/'
                    .$rgDescription['icon_url'].
                    '" alt="'.$rgDescription['market_name'].
                    ' width="80" height="75"></td>');
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM