多维PHP数组 - 从密钥中选择数据

时间:2016-05-25 20:57:23

标签: php html arrays multidimensional-array

不确定我是否正确地标题了这个问题。我在循环使用多维PHP数组来构建一些HTML节点时遇到了一些麻烦。这是我正在循环的数组:

    $locations = array(
      'CityName' => array(
          array(
            'title' => 'Title',
            'phone' => '(555) 555-5555',
            'address' => '1234 Fake st.',
            'city' => 'Ventura',
            'state' => 'CA',
            'zip' => '93003',
            'url' => 'http://www.google.com/'
          ),
          array(
            'title' => 'Title',
            'phone' => '(555) 555-5555',
            'address' => '1234 Fake st.',
            'city' => 'Ventura',
            'state' => 'CA',
            'zip' => '93003',
            'url' => 'http://www.google.com/'
          ),
      ),
      'CityName2' => array(
          array(
            'title' => 'Title',
            'phone' => '(555) 555-5555',
            'address' => '1234 Fake st.',
            'city' => 'Ventura',
            'state' => 'CA',
            'zip' => '93003',
            'url' => 'http://www.google.com/'
          ),
          array(
            'title' => 'Title',
            'phone' => '(555) 555-5555',
            'address' => '1234 Fake st.',
            'city' => 'Ventura',
            'state' => 'CA',
            'zip' => '93003',
            'url' => 'http://www.google.com/'
          )
      )
    );

请记住,我可能已经错误地构建了这个数组,因为我正在尝试做什么。此循环的HTML输出应为:

    <h4>CityName</h4>
    <ul>
      <li>
        <p>Title</p>
        <p>1234 Fake St.</p>
        <p>Ventura, CA 93003</p>
        <p>(555) 555-5555</p>
        <p class="link"><a href="http://www.google.com/" target="_blank">Visit Website</a></p>
      </li>
      <li>
        <p>Title</p>
        <p>1234 Fake St.</p>
        <p>Ventura, CA 93003</p>
        <p>(555) 555-5555</p>
        <p class="link"><a href="http://www.google.com/" target="_blank">Visit Website</a></p>
      </li>
    </ul>
    <h4>CityName2</h4>
    <ul>
    ...
    </ul>

我认为我想要做的是能够抓取单个数据片段以插入我的HTML模板..比如$ location ['title'],$ location ['phone']等.PHP我目前所拥有的只会循环并回显每个单独位置数组中的键或值。

    <?php
    // Printing all the keys and values one by one
    $locationNames = array_keys($locations);

    for($i = 0; $i < count($locations); $i++) {
        echo "<h4>" . $locationNames[$i] . "</h4>";
        echo "<ul>";

        foreach($locations[$locationNames[$i]] as $key => $value) {

            foreach($value as $key => $value) {
              echo $value;
            }

        }

        echo "</ul>";

    }

    ?>

4 个答案:

答案 0 :(得分:0)

这样的事情应该有效。我不会为你实现HTML,但你应该很容易。这样做的好处是,如果你在内部数组中有动态键,你就不必事先了解它们。

foreach($locations as $key => $value) {
    echo $key, PHP_EOL;
    $data = $locations[$key];
    $length = count($data);
    for($i = 0; $i < $length; $i++) {
        $values = $data[$i];
        foreach($values as $key2 => $value2)
            echo "\t", $key2, ": ", $value2, PHP_EOL;
    }
}

答案 1 :(得分:0)

使用嵌套的foreach循环并将值放入适当的位置:

<?php foreach ($locations as $location => $ldata) { ?>

   <h4><?php echo $location; ?></h4>
    <ul>

    <?php foreach ($ldata as $attribute) { ?>

      <li>
        <p><?php echo $attribute['title']; ?></p>
        <p><?php echo $attribute['address']; ?></p>
        <p><?php echo $attribute['city'] . " ," . $attribute['state'] . " " . $attribute['zip']; ?></p>
        <p><?php echo $attribute['phone']; ?></p>
        <p class="link"><a href="<?php echo $attribute['url']; ?>" target="_blank">Visit Website</a></p>
      </li>

    <? php } ?>

<?php } ?>

答案 2 :(得分:0)

你只需要嵌套(foreach)循环:

<?php foreach($locations as $cityname => $location):?>

    <h4><?=$cityname?></h4>
    <ul>
    <?php foreach($location as $place:?>
      <li>
        <p><?=$place['title']?></p>
        <p><?=$place['phone']?></p>
        <!-- etc etc-->
      </li>
      <?php endforeach;?>
    </ul>

<?php endforeach;?>

答案 3 :(得分:0)

只需对您的代码进行一些调整:

<?php
// Printing all the keys and values one by one
$locationNames = array_keys($locations);

for($i = 0; $i < count($locations); $i++) {
    echo "<h4>" . $locationNames[$i] . "</h4>";
    echo "<ul>";

    foreach($locations[$locationNames[$i]] as $key => $value) {
        echo "<li>"; // add list open tag                        <-- tweak #1
        foreach($value as $key => $value) {
          echo "<p>$value</p>"; // add paragraph tags            <-- tweak #2
        }
        echo "</li>"; // add list close tag                      <-- tweak #3
    }

    echo "</ul>";

}

?>

PHP Sandbox example.