Jump to content

Taking single numeric key array and sorting by multi-dimensions.


Go to solution Solved by Jacques1,

Recommended Posts

I have an array like so:

array (
  0 => 
  array (
    'zipcode' => '18640',
    'latitude' => '41.31390',
    'longitude' => '-75.77767',
    'state' => 'Pennsylvania',
    'county' => 'Luzerne',
    'city' => 'Pittston',
  ),
  1 => 
  array (
    'zipcode' => '18515',
    'latitude' => '41.44901',
    'longitude' => '-75.66408',
    'state' => 'Pennsylvania',
    'county' => 'Lackawanna',
    'city' => 'Scranton',
  ),
  2 => 
  array (
    'zipcode' => '18704',
    'latitude' => '41.27534',
    'longitude' => '-75.89115',
    'state' => 'Pennsylvania',
    'county' => 'Luzerne',
    'city' => 'Kingston',
  ),
  3 => 
  array (
    'zipcode' => '90210',
    'latitude' => '34.08476',
    'longitude' => '-118.40629',
    'state' => 'California',
    'county' => 'Los Angeles',
    'city' => 'Beverly Hills',
  ),
  4 => 
  array (
    'zipcode' => '95456',
    'latitude' => '39.26767',
    'longitude' => '-123.75502',
    'state' => 'California',
    'county' => 'Mendocino',
    'city' => 'Little River',
  ),
  5 => 
  array (
    'zipcode' => '98499',
    'latitude' => '47.16618',
    'longitude' => '-122.50846',
    'state' => 'Washington',
    'county' => 'Pierce',
    'city' => 'Lakewood',
  ),
)

and I'm looking to use it to populate a drop-down like so:

 

Pennsylvania (state)

 -Luzerne (county)

  --18640

  --18702

 -Lackawanna (county)

  --18515

 

California

 -Mendocino

  --95456

 -Pierce

  --98499

 

So, I thought if this was accomplished with nested foreach's I'd do something like:

echo '<select>';
foreach ($zipcode as $singlestate) {
  echo '<option id='.$singlestate.'>';

    foreach ($singlestate as $singlecountinsinglestate) {
      echo '<option id='.$singlecountyinsinglestate.'>';

         foreach ($singlecountyinsinglestate as $individualzipcodes) {
           echo '<option id='.$individualzipcodes.'>';
         }

    }

}
echo '</select';

Which would end up looking like so:

<select>
<option id="California">California</option>
<option id="-Mendocino">-Mendocino</option>
<option id="--95456">--95456</option>
<option id="Pennsylvania">Pennsylvania</option>
<option id="-Luzerne">-Luzerne</option>
<option id="--18640">--18640</option>
<option id="--18702">--18702</option>
<option id="-Lackawanna">-Lackawanna</option>
<option id="--18515">--18515</option>
</select>

Of course, that entire idea wouldn't work if, in order to group the original array by State=>County=>ZIP, wasn't done using nested foreach's though... and frankly I can group by state, I can group by county but I get lost when I need to group by State=>County=>ZIP

 

<?php

$raw_data = ...;

$structured_data = [];
foreach ($raw_data as $entry)
{
    $structured_data[$entry['state']][$entry['county']][] = $entry['zipcode'];
}

var_dump($structured_data);

 

Whoa, you just blew my mind....... :-/

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.