PHP:计算并显示多维数组的值[重复]

Posted

技术标签:

【中文标题】PHP:计算并显示多维数组的值[重复]【英文标题】:PHP: Count and display the value of a multidimensional array [duplicate] 【发布时间】:2022-01-12 17:41:43 【问题描述】:

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

数组

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

【问题讨论】:

使用国家作为搜索所以你的意思可能是country 我可以亲自验证英国没有 3 个伦敦,法国没有 2 个巴黎,意大利没有 2 个罗马 这能回答你的问题吗? How to Sort a Multi-dimensional Array by Value 【参考方案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);

将返回:

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
)

编辑 - 添加了来自 $newData 的选择:

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

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

将返回:

这是:

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

【讨论】:

谢谢,如果我想在选择那个新数组的选项中显示它,我应该怎么做? foreach ($newData as $row) echo "&lt;option value='" . $row[] . "'&gt;" . $row[] . "&lt;/option&gt;"; 我不能手动进入这个国家,因为我不“认识”他们 我将添加到我的示例中 - 这是一个两个活套...:) 谢谢@Shlomtzion 我正在尝试在选项选择中输入数量:我插入了arsort($newData),但它不起作用 @Armonius 看看我的最后一次编辑,希望我能找到你.. :)【参考方案2】:
$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
)

【讨论】:

【参考方案3】:

您可以使用array_maparray_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

【讨论】:

以上是关于PHP:计算并显示多维数组的值[重复]的主要内容,如果未能解决你的问题,请参考以下文章

PHP多维数组搜索并获取键的数组[重复]

php多维数组计数重复值并添加对应值

php 多个多维数组求交集

如果两个值匹配,则从 php 中的多维关联数组中删除重复项

PHP循环遍历多维数组并更改值

php计算多维数组某个值的和