PHP |获取集合大小

时间:2017-03-22 00:45:11

标签: php html

我有以下php数组:

{
  "Like New": [
    {
      "id": 1,
      "title": "Computer",
    }
  ],
  "New": [
    {
      "id": 2,
      "title": "Refrigerator",
    },
    {
      "id": 3,
      "title": "Car",
    }
  ]
}

我需要在HTML页面中打印下表。我怎么能这样做:

Condition  | Count
-----------|--------
Like New   | 1
New        | 2 

2 个答案:

答案 0 :(得分:0)

这些方面应该做的事情:

foreach($data as $key=>$value){
   echo $key." | ".count($value);
}

答案 1 :(得分:0)

鉴于此阵列...

$items = array(
    'Like New' => array(
        array(
            'id'=>1,
            'title' => 'computer'
        )
    ),
    'New' => array(
        array(
            'id'=>2,
            'title' => 'Refrigerator'
        ),
        array(
            'id'=>3,
            'title' => 'Car'
        )
    )
);

这样做......

$html = '<table>
    <thead>
        <tr>
            <th>Condition</th>
            <th>Count</th>
        </tr>
    </thead>
    <tbody>';

foreach($items as $key=>$value)
{

    $html .= '
    <tr>
        <td>' . $key . '</td>
        <td>' . count($items[$key]) . '</td>
    </tr>';

}

$html .= '
    </tbody>
</table>';

echo $html;

<强>结果:

+-----------+-------+
| Condition | Count |
+-----------+-------+
| Like New  |     1 |
| New       |     2 |
+-----------+-------+