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?

    This should break it roughly in the middle, while not breaking in the middle of a state (untested, though):

    <?php
    $arr = array();
    $count = 0;
    foreach($locations as $store) {
       $arr[$store['location_state']][] = $store['location_title'];
       $count++;
    }
    $half = ceil($count / 2);
    $count = 0;
    $col = 1;
    echo "<div id='col1'>\n";
    foreach($arr as $stateName => $title) {
       echo '<h3>' . $stateName . '</h3>';
       echo '<ul>' . "\n";
       foreach($title as $key => $titleName) {
          echo '<li>' . $titleName . '</li>' . "\n";
       }
       echo '</ul>';
       if ($col == 1 && ++$count >= $half) {
          $col = 2;
          echo "</div>\n<div id='col2'>\n";
       }
    }
    echo "</div>\n";
    

      Thanks NogDog. It didn't quite work right out of the box, but you got me on the right path. I made some adjustments to allow me to change the number of columns on the fly if I need to.

      I went ahead and put this in a function to keep things tidier.

      Here is the final code and thanks again.

      function buildList($data, $columns) {
      $arr = array();
      	$count = 0;
      	foreach ($data as $stores) {
      		$arr[$stores['location_state']][] = $stores['location_title'];
      		$count++;
      	}
      
      $rowCount = ceil($count / $columns);
      $count = 0;
      $col = 1;
      
      $theList = '<div class="column first">' . "\n";
      foreach ($arr as $stateName => $cities) {
      	$theList .= '<h3>'.$stateName.'</h3>' . "\n";
      	$theList .= '<ul>' . "\n";
      		foreach ($cities as $key => $cityName) {
      			$theList .= '<li>'.$cityName.'</li>' . "\n";
      
      			if (++$count % $rowCount == 0) {
      				$col++;
      				$colClass = ($col == $columns) ? 'column last' : 'column';
      				$theList .= '</ul>' . "\n";
      				$theList .= '</div>' . "\n";
      				$theList .= '<div class="'.$colClass.'">' . "\n";
      				$theList .= '<ul>' . "\n";
      
      			}
      		}
      	$theList .= '</ul>' . "\n";
      }
      $theList .= '</div>' . "\n";
      
      return $theList;
      }
      
      

        Thanks for following up with the working code. 🙂

          Write a Reply...