LeMoussel LeMoussel - 1 year ago 101
PHP Question

Dividing Array Into 3 Equal Columns

I have a dynamic number of items in which I'll need to divide into 3 columns. Let's say I'm given this:

array("one", "InfoOne", "LibOne",
"two", "InfoTwo", "LibTwo",
"three", "InfoThree", "LibThree")

I need to generate array like this:

[0] = array("one", "InfoOne", "LibOne"),
[1] = array("two", "InfoTwo", "LibTwo"),
[2] = array("three", "InfoThre", "LibThree")

How could I do to put in an array, data with 3 equal columns?

Answer Source

Using array_chunk() you can do this-

$ori = array("one", "InfoOne", "LibOne", 
      "two", "InfoTwo", "LibTwo", 
      "three", "InfoThree", "LibThree");
$chunked = array_chunk($ori, 3);
echo '<pre>';
echo '</pre>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download