PHP问题:无法通过关联数组循环打印白色的行号

时间:2019-07-03 20:55:26

标签: php foreach

我正在PHP simplexml_load_file的帮助下进行基本的XML解析操作。

我遍历了“ CD目录”,并在HTML表中打印出了歌曲名称,艺术家等。鉴于我的大小,给定XML文件中信息的结构,我无法打印行号

代码如下:

<?php
  $catalog = simplexml_load_file('https://www.w3schools.com/xml/cd_catalog.xml');
  $cds = $catalog->CD;
?>

<table class="table table-striped table-bordered">
  <thead>
    <tr>
      <th>#</th>
      <th class="text-left">Song</th>
      <th class="text-left">Artist</th>
      <th class="text-left">Year</th>
      <th class="text-left">Price</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($cds as $k=>$cd) { ?>
    <tr>
      <td><?php echo $k; ?></td>
      <td><?php echo $cd->TITLE; ?></td>
      <td><?php echo $cd->ARTIST; ?></td>
      <td><?php echo $cd->YEAR; ?></td>
      <td><?php echo $cd->PRICE; ?></td>
    </tr>
    <?php } ?>
  </tbody>
</table>

第一列打印“ CD”而不是行号。

要更改行号我应该怎么做?

2 个答案:

答案 0 :(得分:1)

您好,这是一个简单的解决方案,只需将xml对象作为字符串转换为新数组并循环:

<?php
$catalog = simplexml_load_file('https://www.w3schools.com/xml/cd_catalog.xml');
$cds = $catalog->CD;
$customCatalog = array();
foreach ($cds as  $value) {
    $temp = array(
        'TITLE' =>$value->TITLE->__toString() ,
        'ARTIST'=> $value->ARTIST->__toString(),
        'COUNTRY'=>$value->COUNTRY->__toString(),
        'COMPANY'=>$value->COMPANY->__toString(),
        'PRICE'=>$value->PRICE->__toString(),
        'YEAR'=>$value->YEAR->__toString()
    );
    $customCatalog [] = $temp;
}

//print_r($customCatalog);

foreach ($customCatalog as $key => $value) {
    if (isset($value)) {
        echo $key;
        echo $value["TITLE"];
        echo $value["ARTIST"];
        echo $value["COUNTRY"];
        echo $value["COMPANY"];
        echo $value["PRICE"];
        echo $value["YEAR"];
    }
}
?>

希望有帮助

答案 1 :(得分:0)

如安德烈亚斯(Andreas)所建议,我做了$rowCount = 1;并在每次迭代中将其递增:

<?php
    $catalog = simplexml_load_file('https://www.w3schools.com/xml/cd_catalog.xml');
    $cds = $catalog->CD;
    $rowCount = 1;
?>

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My favourite songs</title>
  <meta name="description" content="Songs">
  <meta name="author" content="">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body>
  <div class="panel panel-default table-container">
    <div class="panel-heading clearfix">Great songs</div>
    <div class="panel-body">
      <div class="table-responsive">
        <table class="table table-striped table-bordered">
          <thead>
            <tr>
              <th>#</th>
              <th class="text-left">Song</th>
              <th class="text-left">Artist</th>
              <th class="text-left">Year</th>
              <th class="text-left">Price</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($cds as $k=>$cd) { ?>
            <tr>
              <td><?php echo  $rowCount++; ?></td>
              <td><?php echo $cd->TITLE; ?></td>
              <td><?php echo $cd->ARTIST; ?></td>
              <td><?php echo $cd->YEAR; ?></td>
              <td><?php echo $cd->PRICE; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</body>
</html>

有phpFiddle here

还是,有一种解决方案仅涉及$catalog数组吗?