LeMoussel LeMoussel - 1 month ago 9
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:

array(
[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

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>';
print_r($chunked);
echo '</pre>';
Comments