This question has been asked a couple of different ways probably. But not like I'm needing below.
I am retrieving data from a table via fetchAll. This turns my query into a multidimensional array.
Array
(
[0] => Array
(
[location_title] => Name 1
[location_state] => State 1
)
[1] => Array
(
[location_title] => Name 2
[location_state] => State 1
)
[2] => Array
(
[location_title] => Name 3
[location_state] => State 2
)
I then use this bit of code to organize and group the array by state.
$arr = array();
foreach ($locations as $store) {
$arr[$store['location_state']][] = $store['location_title'];
}
foreach ($arr as $stateName => $title) {
echo '<h3>'.$stateName.'</h3>';
echo '<ul>' . "\n";
foreach ($title as $key => $titleName) {
echo '<li>'.$titleName.'</li>' . "\n";
}
echo '</ul>';
}
I get a nice clean list.
State Name
Name 1
Name 2
State name
Name 3
These lists are quite a bit longer in reality. I would like to split them up in half and display them as two equal sized columns instead of one long list. I have tried doing a count and dividing by 2, array_chunk, and messed around with a modulus approach.
It seems that the first foreach is splitting the arrays into two separate lists. Which is where I'm struggling to see the solution. The list needs to break mid way through the the first loop in the foreach. I don't seem to be able to get this to happen. The best I've been able to do is create columns according to each state grouping.
Any ideas?