0

I'm experimenting a bit with multidimensional arrays but I can't get certain values to be displayed: for example, using the Nation as a search, I would like to group by "city" and count how many are in common. For example:

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

Example of the result:

ENGLAND - LONDON    - 3
FRANCE  - PARIS     - 2
ITALY   - ROME      - 2
ENGLAND - LIVERPOOL - 1
ENGLAND - BRISTOL   - 1
  • _using the Nation as a search_ So you perhaps mean the `country` – RiggsFolly Dec 07 '21 at 16:30
  • I can personally verify that there are NOT 3 Londons in England or 2 Paris's in France or 2 Rome's in Italy – RiggsFolly Dec 07 '21 at 16:32
  • 1
    Does this answer your question? [How to Sort a Multi-dimensional Array by Value](https://stackoverflow.com/questions/2699086/how-to-sort-a-multi-dimensional-array-by-value) – Aaron Morefield Dec 07 '21 at 18:06

3 Answers3

0

Simple enough here is an example:

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

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

Edit - Added select from $newData:

Example of select - just to illustrate how to loop and get your values and keys:

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

Will return:

enter image description here

Which is:

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

Edit - if you want this: all data sorted and countries

enter image description here Do this:

add

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

to the first foreach after $sorted...

after the loop add:

arsort($sorted2);

And in the select loop do this:

?><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
Shlomtzion
  • 674
  • 5
  • 12
  • Thank you, if I want to display it in an option of a select that new array how should I do? `foreach ($newData as $row) { echo ""; } ` I can't enter the country manually because I don't "know" them –  Dec 09 '21 at 11:41
  • 1
    I will add to my example - it's a two looper...:) – Shlomtzion Dec 09 '21 at 13:46
  • I'm trying to order to enter quantity in Option Select: I inserted the `arsort($newData)` but it doesn't work –  Dec 09 '21 at 14:40
  • could you explain a bit clearer... what should an option have in its value its name and its text and what are the options sorted by? – Shlomtzion Dec 09 '21 at 15:47
  • the final result I want to achieve is this, in the select > options: LONDON (30) - LIVERPOOL (15) - BRISTOL (10) - PARIS (5) - ROME (2) –  Dec 09 '21 at 16:39
  • 1
    @Armonius See my last edit, I hope I got you .. :) – Shlomtzion Dec 11 '21 at 22:02
  • Thank you :) Now I'm trying to order just having more foreach, for example: `foreach ($newData as $country => $ctr) { foreach ($ctr as $city => $cit) { foreach ($cit as $opt => $qt) {echo ""; }}}` How do i order in descending order `$qt`? –  Dec 14 '21 at 15:46
  • `$qt` is a number @Shlomtzion –  Dec 14 '21 at 16:26
  • Why not use the $sorted2 array as I have demonstrated? this way you will have it sorted already... and Quantity is setup in also... – Shlomtzion Dec 15 '21 at 18:43
  • Because I need all the values to insert in the options, if I use $sorted2 I lose the $country values. If I use 3 foreach I don't lose any value –  Dec 17 '21 at 08:42
  • Ok , gottcha, np you don't need more loops I edited my answer again with a revised solution. – Shlomtzion Dec 19 '21 at 12:28
  • The country is a value we can keep in another array... we don't need to keep all values in the main array... country is a value that has nothing to do with the location of it in the sort.... we can just hold it keyed to the city...:) – Shlomtzion Dec 19 '21 at 12:42
0
$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);

RESULT

Array
(
    [ENGLAND-LONDON] => 3
    [FRANCE-PARIS] => 2
    [ITALY-ROME] => 2
    [ENGLAND-LIVERPOOL] => 1
    [ENGLAND-BRISTOL] => 1
)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

You can make this a one liner with array_map and array_count_values.

Use array_map to loop over the array and concat each country and city with a delimiter, say -. Now, pass it's result to array_count_values to get the frequency of each key.

<?php

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

Online Demo

nice_dev
  • 17,053
  • 2
  • 21
  • 35