Split Array into 2 'chunks'?

Hi,

I’d like my one array to be split into 2 arrays.

Currently it outputs HTML:


<ul>
<li>item 1</li>
<li>item 2</li>
</ul>

However, I’d like to have:


<ul>
<li>item 1</li>
</ul>
<ul>
<li>item 2</li>
</ul>

My current code is:


                    $delimiter = ", ";
                    $array = explode($delimiter, $entries['0']['features']);                    
                    if ($array != null) {
                    echo '<ul style="margin-left:16px">';
                      foreach ($array as $value) {
                          echo "<li>".$value."</li>\
";
                      }    
                    echo '</ul>';
                    }

FYI $entries[‘0’][‘features’] above stores a bunch of CSVs.

Any help would be super greatly appreciated.

Thank you!!!

array_chunk()?

Cool.

I’m using this:


print_r(array_chunk($array, 2, true));

Can you explain how I now take the 2 arrays and display them in their own unordered list?

Thank you

Based on the sample, you would just more the UL echos inside the foreach loop.


                    $delimiter = ", ";
                    $array = explode($delimiter, $entries['0']['features']);                    
                    if ($array != null) {
                      foreach ($array as $value) {
                    echo '<ul style="margin-left:16px">';
                          echo "<li>".$value."</li>\
";
                    echo '</ul>';
                      }    
                    }

Hi Dave,

Sorry my example is misleading.

I meant that I’d just like to split all the results into 2 columns. So basically a total of 2 <UL>s

Can anyone help with this?

I’m just looking to take all the array data and split it over 2 <ul>s.

Thanks for any help

Use array_chunk() but make the chunk size half (or the nearest bigger/smaller whole number depending on which side can have one more/less items) of the size of the array. For example, with an array like array('a','b','c','d','e','f'), which has 6 items, set the chunk size to 3.

Thanks for the response, sir :slight_smile:

Aaaah, so I should count the no. of array items and then use array_chunk()?

Sure, give it a go!

Hi :slight_smile:

Using this:


$delimiter = ", ";
$array = explode($delimiter, $entries['0']['features']);
print_r(array_chunk($array, 3, true));

My output is:


Array
(
    [0] => Array
        (
            [0] => Parking
            [1] => gym
            [2] => shop
        )
    [1] => Array
        (
            [3] => internet
            [4] => laundry room
            [5] => common room
        )
)

How do I now take these arrays and place them in separate <ul>s?

I’m just a bit stuck :cry: Thanks for any help.

Come Michael, stop being lazy - you know how to do this. :smiley:

Forget about everything you’ve done already, you have this array structure, to get the two UL’s you would…

:smiley: Hot dang! I’ve been fun oot.

Ha ha.

So, what do you have now? :slight_smile:

My mind is a mess of foreach loops and multiple arrays…

You have an array with 2 elements, and each element has 3 items.

You need 2 lists, each with 3 items.

Come on… :stuck_out_tongue:

Lunch break!!! :smiley:

I’ll tackle this shortly after lunchakins and promise you’ll be amazed by the technicolor poetry therein…at least I hope :wink:

I’m starting to feel a little guilty now. :frowning:


foreach(array_chunk($array, 3) as $list){
  echo '<ul>', PHP_EOL;
  foreach($list as $item){
    echo "\	", '<li>', $item, '</li>', PHP_EOL;
  }
  echo '</ul>', PHP_EOL;
}

/*
  <ul>
    <li>parking</li>
    <li>gym</li>
    <li>shop</li>
  </ul>
  <ul>
    <li>internet</li>
    <li>common room</li>
    <li>laundry room</li>
  </ul>
*/

Ne’er feel guilty, kind sir.
I can figure it out pseudo code styles but me brain ain’t what it were.

That code worked perfectly my good man. I did actually almost crack at it earlier on, but just ended up with a bunch of unclosed <ul>s.

Thanks again m’laud