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
3
  • using the Nation as a search So you perhaps mean the country Commented Dec 7, 2021 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 Commented Dec 7, 2021 at 16:32
  • 1
    Does this answer your question? How to Sort a Multi-dimensional Array by Value Commented Dec 7, 2021 at 18:06

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

12 Comments

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 "<option value='" . $row[] . "'>" . $row[] . "</option>"; } I can't enter the country manually because I don't "know" them
I will add to my example - it's a two looper...:)
I'm trying to order to enter quantity in Option Select: I inserted the arsort($newData) but it doesn't work
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?
@Armonius See my last edit, I hope I got you .. :)
|
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
)

Comments

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

Comments