如何以表格的形式安排PHP数组

时间:2014-05-12 22:48:15

标签: php html arrays

我正在使用他们的POS的API为当地的寄售商店开发搜索。我的代码使用以下代码提取数据:

$search = $_GET['search'];
$data = array('key' => $API_KEY,
          /*'consignorId' => '1',*/
          'query' => $search,
          'includeItemsWithQuantityZero' => 'false');

$data_string = json_encode($data);

$context = stream_context_create(array(
'http' => array(
    'method' => "POST",
    'header' => "Accept: application/json\r\n".
                "Content-Type: application/json\r\n",
    'content' => $data_string
)
));

$result = file_get_contents('https://user.traxia.com/app/api/inventory', false, $context);

返回json输出:

{"results":[{"name":"BKE ","category":"Jeans","sku":"64SA6Z","description":"","color":"blue","size":"4","consignorId":"20579","expireDate":"08/10/2014","startDate":"05/12/2014","status":"ACTIVE","quantity":1,"cost":0,"retail":2500,"discount":0,"buyersFee":200,"images":[],"consignmentItem":true,"doNotDiscount":false,"currentPrice":2700},{"name":"BKE","category":"Shorts","sku":"EUTU4Z","description":"","color":"blue","size":"10","consignorId":"687517","expireDate":"08/07/2014","startDate":"05/09/2014","status":"ACTIVE","quantity":1,"cost":0,"retail":1000,"discount":0,"buyersFee":90,"images":[],"consignmentItem":true,"doNotDiscount":false,"currentPrice":1090},

我使用此代码解码:

$jsonData = $result;
$phpArray = json_decode($jsonData, true);
$phpArray = $phpArray['results'];
foreach ($phpArray as $key => $value) { 
    print_r("<h2>$key</h2>");
    foreach ($value as $k => $v) { 
        print_r("$k | $v <br />");
    }
}

返回一个看起来像的页面(所有结果都是这样):

0

名称| BKE
类别|牛仔裤sku | 64SA6Z
描述|
颜色|蓝色尺寸| 4
发货人| 20579
expireDate | 08/10/2014
startDate | 05/12/2014
状态|活动数量| 1成本| 0
零售| 2500
折扣| 0
买家费| 200
图像|数组
consignmentItem | 1
doNotDiscount |
currentPrice | 2700

我想把它排成一张桌子。有没有一种简单的方法可以做到这一点/甚至可能吗?谢谢!

2 个答案:

答案 0 :(得分:1)

在循环中添加表结构非常简单 -

foreach ($phpArray as $key => $value) { 
    print_r("<h2>$key</h2>");
    echo "<table>";
    foreach ($value as $k => $v) { 
        print_r("<tr><td>$k</td><td>$v </td></tr>");
    }
    echo "</table>";
}

或者,如果您希望同一行中的所有信息(假设有多个项目显示):

echo "<table>";
foreach ($phpArray as $key => $value) { 
    print_r("<tr><td>$key</td>");
    foreach ($value as $k => $v) { 
        print_r("<td>$k</td><td>$v</td>");
    }
    echo "</tr>";
}
echo "</table>";

答案 1 :(得分:0)

你想写这样的东西

  <table border="1" cellpadding="8" cellspacing="0" style="border-collapse: collapse">
     <!-- border, cellspacing, and cellpadding attributes are not recommended; only included for example's conciseness -->
     <thead>
        <tr>
           <?php
           foreach(array_keys($phpArray[0]) as $k) {
              echo "<th>$k</th>";
           }
           ?>
        </tr>
     </thead>
     <tbody>
        <?php
        foreach($phpArray as $key => $values) {
           echo '<tr>';
           foreach($values as $v) {
              echo "<td>" . implode('<br /> ', (array)$v) . "</td>";
           }
           echo '</tr>';
        }
        ?>
     </tbody>
  </table>