PHP:统计并显示多维数组的值

PHP: Count and display the value of a multidimensional array

我正在尝试使用多维数组,但无法显示某些值:例如,使用“国家”作为搜索条件,我想按“城市”分组并计算有多少个共同点。例如:

数组

Array(
[0] => Array
    (
        [city] => LONDON
        [country] => ENGLAND
    )

[1] => Array
    (
        [city] => LONDON
        [country] => ENGLAND
    )

[2] => Array
    (
        [city] => LONDON
        [country] => ENGLAND
    )

[3] => Array
    (
        [city] => PARIS
        [country] => FRANCE
    )

[4] => Array
    (
        [city] => LIVERPOOL
        [country] => ENGLAND
    )

[5] => Array
    (
        [city] => ROME
        [country] => ITALY
    )

[6] => Array
    (
        [city] => ROME
        [country] => ITALY
    )

[7] => Array
    (
        [city] => PARIS
        [country] => FRANCE
    )

[8] => Array
    (
        [city] => BRISTOL
        [country] => ENGLAND
    )
)

结果示例:

ENGLAND - LONDON    - 3
FRANCE  - PARIS     - 2
ITALY   - ROME      - 2
ENGLAND - LIVERPOOL - 1
ENGLAND - BRISTOL   - 1

例子很简单:

<?php

/*Create the array for example */
$data[0]['city'] = "LONDON";
$data[0]['country'] = "ENGLAND";
$data[1]['city'] = "LONDON";
$data[1]['country'] = "ENGLAND";
$data[2]['city'] = "LONDON";
$data[2]['country'] = "ENGLAND";
$data[3]['city'] = "PARIS";
$data[3]['country'] = "FRANCE";
$data[4]['city'] = "LIVERPOOL";
$data[4]['country'] = "ENGLAND";
$data[5]['city'] = "ROME";
$data[5]['country'] = "ITALY";
$data[6]['city'] = "ROME";
$data[6]['country'] = "ITALY";
$data[7]['city'] = "PARIS";
$data[7]['country'] = "FRANCE";
$data[8]['city'] = "BRISTOL";
$data[8]['country'] = "ENGLAND";

/* print the array for show 
echo '<pre>';
print_r($data);*/

foreach($data as $val){
    if(!isset($newData[$val['country']][$val['city']])){
        $newData[$val['country']][$val['city']] = 1;
    } else {
        ++$newData[$val['country']][$val['city']];
    }
    $sorted["{$val['country']} - {$val['city']}"] = $newData[$val['country']][$val['city']];
}

/* print created array */
echo '<pre>';
print_r($newData);

/* print the sorted array */
arsort($sorted);
print_r($sorted);

会 return:

Array
(
    [ENGLAND] => Array
        (
            [LONDON] => 3
            [LIVERPOOL] => 1
            [BRISTOL] => 1
        )

    [FRANCE] => Array
        (
            [PARIS] => 2
        )

    [ITALY] => Array
        (
            [ROME] => 2
        )

)
Array
(
    [ENGLAND - LONDON] => 3
    [FRANCE - PARIS] => 2
    [ITALY - ROME] => 2
    [ENGLAND - LIVERPOOL] => 1
    [ENGLAND - BRISTOL] => 1
)

编辑 - 添加了 select 来自 $newData:

select 的示例 - 只是为了说明如何循环并获取您的值和键:

?><select id="city" name="city"><?php
foreach($newData as $country => $cities){
    
    foreach($cities as $city => $quantity){
        ?>
          <option id="<?php echo $city;?>" value='<?php echo "$country-$city";?>' ><?php echo $city;?></option>
       <?php
    }
    
}
?></select><?php

会 return:

即:

<select id="city" name="city">        
    <option id="LONDON" value="ENGLAND-LONDON">LONDON</option>    
    <option id="LIVERPOOL" value="ENGLAND-LIVERPOOL">LIVERPOOL</option>    
    <option id="BRISTOL" value="ENGLAND-BRISTOL">BRISTOL</option>   
    <option id="PARIS" value="FRANCE-PARIS">PARIS</option>  
    <option id="ROME" value="ITALY-ROME">ROME</option>
</select>

编辑 - 如果您需要:所有数据排序和国家/地区

这样做:

添加

$sorted2[$val['city']] = $newData[$val['country']][$val['city']];
$countries[$val['city']] = $val['country'];

到 $sorted 之后的第一个 foreach...

循环后添加:

arsort($sorted2);

然后在 select 循环中执行此操作:

?><select id="city" name="city"><?php
foreach($sorted2 as $city => $quantity){
        ?>
            <option id="<?php echo $city;?>" value='<?php echo $city;?>' ><?php echo "$city $countries[$city] ($quantity)";?></option>
        <?php
}
?></select><?php
$arr = [
            ['city' => 'LONDON', 'country' => 'ENGLAND'],
            ['city' => 'LONDON', 'country' => 'ENGLAND'],
            ['city' => 'LONDON', 'country' => 'ENGLAND'],
            ['city' => 'PARIS', 'country' => 'FRANCE'],
            ['city' => 'LIVERPOOL', 'country' => 'ENGLAND'],
            ['city' => 'ROME', 'country' => 'ITALY'],
            ['city' => 'PARIS', 'country' => 'FRANCE'],
            ['city' => 'ROME', 'country' => 'ITALY'],
            ['city' => 'BRISTOL', 'country' => 'ENGLAND']            
];

$new = [];
foreach ($arr as $a){
    if ( isset($new[ $a['country'] . '-' . $a['city'] ]) ) {
        $new[ $a['country'] . '-' . $a['city'] ] += 1;
    } else {
        $new[ $a['country'] . '-' . $a['city'] ] = 1;    
    }
}

arsort($new);
print_r($new);

结果

Array
(
    [ENGLAND-LONDON] => 3
    [FRANCE-PARIS] => 2
    [ITALY-ROME] => 2
    [ENGLAND-LIVERPOOL] => 1
    [ENGLAND-BRISTOL] => 1
)

你可以用 array_map and array_count_values.

把它做成一个衬垫

使用array_map遍历数组并用分隔符连接每个countrycity,比如-。现在,将它的结果传递给 array_count_values 以获得每个键的频率。

<?php

print_r(array_count_values(array_map(fn($val) => $val['country'] . ' - '. $val['city'] , $data)));

Online Demo