从数组元素中遍历数组时如何进行标题

时间:2019-04-22 12:03:20

标签: php arrays multidimensional-array

我有一个数组,其中包含多个属性,例如国家/地区,州/省/电话号码。我的问题是,我想让国家成为拥有与以下国家相同的国家的元素:

   California (This should be heading)
   Los Angeles
   San Diego
   San Francisco

我的数组如下:

Array
    (
        [0] => Array
            (
                [id] => 14
                [post_id] => 319
                [location] => Atlanta, GA
                [address] => 161 Racetrack Rd., McDonough
                [city] => Atlanta
                [state] => Georgia
                [state_code] => GA
                [country] => USA
                [zipcode] => 30253
                [service_zipcode] => 
                [phone] => 
                [fax] => 
                [email] => 
                [facebook] => 
                [twitter] => 
                [linkedin] => 
                [gplus] => 
                [latitude] => 
                [longitude] => 
                [url] => 
                [created_at] => 2019-04-22 07:22:55
                [updated_at] => 2019-04-22 07:26:31
            )

        [1] => Array
            (
                [id] => 16
                [post_id] => 321
                [location] => Augusta, Georigia
                [address] => 1446 Harper Street
                [city] => Augusta
                [state] => Georigia
                [state_code] => GA
                [country] => USA
                [zipcode] => 
                [service_zipcode] => 
                [phone] => 
                [fax] => 
                [email] => 
                [facebook] => 
                [twitter] => 
                [linkedin] => 
                [gplus] => 
                [latitude] => 
                [longitude] => 
                [url] => 
                [created_at] => 2019-04-22 07:29:33
                [updated_at] => 2019-04-22 07:32:25
            )

        [2] => Array
            (
                [id] => 12
                [post_id] => 317
                [location] => Savannah, Georgia
                [address] => 834 Northside Dr. East
    Statesboro
                [city] => Savannah
                [state] => Georgia
                [state_code] => GA
                [country] => USA
                [zipcode] => 30458
                [service_zipcode] => 
                [phone] =>
                [fax] => 
                [email] => 
                [facebook] => 
                [twitter] => 
                [linkedin] => 
                [gplus] => 
                [latitude] => 
                [longitude] => 
                [url] => 
                [created_at] => 2019-04-22 07:15:43
                [updated_at] => 2019-04-22 07:18:11
            )

    )

因此,当有州的城市相同时,应首次将州显示为标题。我的数组只有一个状态的数据,但确实要考虑多个状态的数据。我有什么办法可以为此做一个独特的标题吗?

更新:我的代码是

<div class="row">
            <div class="col-md-12 contentside">



                    <div class="locations-container">
                       <?php 
                        global $wpdb;

                        $querystr = "
                            SELECT DISTINCT meta_value 
                            FROM $wpdb->postmeta 
                            WHERE meta_key = 'state_code' 
                            ORDER BY meta_value ASC
                        ";

                        $us_states = $wpdb->get_results( $querystr );
                        //echo "<pre>"; print_r($us_states); echo "<pre>";
                        foreach($us_states as $us_state) { ?>
                            <?php $locations = \abc\Helper\Location::getLocationsByState($us_state->meta_value); ?>
                            <h2>Here I want to display heading for state but it is outside loop</h2>
                            <?php //echo "<pre>"; print_r($locations); echo "</pre>"; ?>
                            <?php foreach($locations as $location): ?>
                                <div class="location-single">
                                    <h3>Mosquito Hunters of <?php echo $location['location']; ?></h3>
                                    <div class="location-inner">
                                        <img src="/wp-content/uploads/2019/04/eco-freeze-bed-bugs.jpg" alt=" ">
                                        <p> <?php echo $location['city'] ?>, <?php echo $location['state_code'] ?><br /> 
                                        <a href="mailto:<?php echo $location['email'] ?>"><?php echo $location['email'] ?></a><br />
                                        <a href="tel:<?php echo $location['phone'] ?>"><?php echo $location['phone'] ?></a></p>
                                        <a href="<?php echo $location['url'] ?>" class="btn">Learn about this location</a>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                       <?php } ?>
                    </div>
            </div>
        </div>

2 个答案:

答案 0 :(得分:2)

如果使用array_column,则可以得到一个以状态为值,城市为键的平面数组。
然后使用asort对数组进行排序以保留键并按状态对其进行排序。

跟踪州的变化情况,并回显州下的城市。

$arr = array_column($locations, "state", "city");
asort($arr);
$prev = null;

foreach($arr as $city => $state){
    if($state != $prev){
        echo PHP_EOL . PHP_EOL . $state . PHP_EOL;
        $prev = $state;
    }
    echo "---" . $city . PHP_EOL;
}

https://3v4l.org/gfcia

示例输出:

Georgia
---Atlanta
---Savannah


Georigia
---Augusta


Texas
---Huston

答案 1 :(得分:0)

实施步骤:

1)请循环遍历当前数组。

2)创建一个新的空白数组。

3)以country作为键附加到新数组,

4)以state作为键追加到新数组,

现在,循环遍历新数组,并在单独的行上打印国家和州。

<?php
$locations = Array(
        Array
            (
                'id' => 14,
                'post_id' => 319,
                'location' => 'Atlanta, GA',
                'address' => '161 Racetrack Rd., McDonough',
                'city' => 'Atlanta',
                'state' => 'Georgia',
                'state_code' =>'GA',
                'country' => 'USA',
                'zipcode' => 30253,
                'service_zipcode' => '',
                'phone' => '',
                'fax' => '',
                'email' => '',
                'facebook' => '',
                'twitter' => '',
                'linkedin' => '',
                'gplus' => '',
                'latitude' => '',
                'longitude' => '',
                'url' => '',
                'created_at' => '2019-04-22 07:22:55',
                'updated_at' => '2019-04-22 07:26:31',
            ),
            Array
            (
                'id' => 16,
                'post_id' => 321,
                'location' => 'Augusta, Georigia',
                'address' => '1446 Harper Street',
                'city' => 'Augusta',
                'state' => 'Georigia',
                'state_code' => 'GA',
                'country' => 'USA',
                'zipcode' => '',
                'service_zipcode' => '',
                'phone' => '',
                'fax' => '',
                'email' => '',
                'facebook' => '',
                'twitter' => '',
                'linkedin' => '',
                'gplus' => '',
                'latitude' => '',
                'longitude' => '',
                'url' => '',
                'created_at' => '2019-04-22 07:29:33',
                'updated_at' => '2019-04-22 07:32:25',
            ),
            Array
            (
                'id' => 12,
                'post_id' => 317,
                'location' => 'Savannah, Georgia',
                'address' => '834 Northside Dr. East, Statesboro',
                'city' => 'Savannah',
                'state' => 'Georgia',
                'state_code' => 'GA',
                'country' => 'USA',
                'zipcode' => 30458,
                'service_zipcode' => '',
                'phone' => '',
                'fax' => '',
                'email' => '',
                'facebook' => '',
                'twitter' => '',
                'linkedin' => '',
                'gplus' => '',
                'latitude' => '',
                'longitude' => '',
                'url' => '',
                'created_at' => '2019-04-22 07:15:43',
                'updated_at' => '2019-04-22 07:18:11',
            )

    );
$hierarchy = [];
if (! empty($locations)) {
 foreach ($locations as $location) {
  $hierarchy[$location['country']][$location['state']] = $location;
 }
}

if (! empty($hierarchy)) {
 foreach ($hierarchy as $country => $location) {
  echo "<br/>" . $country;
  if (! empty($hierarchy[$country])) {
   foreach ($hierarchy[$country] as $state => $city) {
    echo "<br/>--> " . $state;
    echo "<br/>----> " . $city['city'];
   }
  }
 }
}

输出:

USA
--> Georgia
----> Savannah
--> Georigia
----> Augusta

Where `USA` -> Country Name
Starting with `-->`: state name
Starting with `---->`: city name

Live Demo: