简体   繁体   中英

Multi-Dimensional PHP array – select data from key

Not sure if I titled this question correctly. I'm having some trouble looping over a multi-demensional php array to build some HTML nodes. Here is the array I'm looping over:

    $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/'
          )
      )
    );

Keep in mind I may have built this array incorrectly for what I'm trying to do. The HTML output for this loop should be:

    <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>

I think what I want to do is to be able to grab the individual pieces of data to plug into my HTML template.. like $location['title'], $location['phone'], etc. The PHP that I currently have will only go as far to loop over and echo out the keys or values from each individual location array.

    <?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>";

    }

    ?>

Something like this should work. I won't implement the HTML for you, but you it should be easy to do. This has the advantage that if you have dynamic keys in the inner array, you won't have to know them before hand.

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;
    }
}

Use nested foreach loops amd drop the values in to the appropriate places:

<?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 } ?>

You just need nested (foreach) loops:

<?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;?>

Just a few tweaks to your code:

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM