alturic Posted August 31, 2016 Share Posted August 31, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302049-taking-single-numeric-key-array-and-sorting-by-multi-dimensions/ Share on other sites More sharing options...
Solution Jacques1 Posted August 31, 2016 Solution Share Posted August 31, 2016 <?php $raw_data = ...; $structured_data = []; foreach ($raw_data as $entry) { $structured_data[$entry['state']][$entry['county']][] = $entry['zipcode']; } var_dump($structured_data); 1 Quote Link to comment https://forums.phpfreaks.com/topic/302049-taking-single-numeric-key-array-and-sorting-by-multi-dimensions/#findComment-1536884 Share on other sites More sharing options...
alturic Posted August 31, 2016 Author Share Posted August 31, 2016 <?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....... :-/ Quote Link to comment https://forums.phpfreaks.com/topic/302049-taking-single-numeric-key-array-and-sorting-by-multi-dimensions/#findComment-1536885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.