Reputation: 5767
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?
Upvotes: 1
Views: 51
Reputation: 3178
Basically, do this:
$array = [
"one", "InfoOne", "LibOne",
"two", "InfoTwo", "LibTwo",
"three", "InfoThree", "LibThree"];
print_r($new_array = array_chunk($array,3,true));
Should provide a nice new array with 3 and 3 values in multidimensional array.
Upvotes: 1
Reputation: 2096
try this....
$t1=array("one", "InfoOne", "LibOne",
"two", "InfoTwo", "LibTwo",
"three", "InfoThree", "LibThree");
print_R(array_chunk($t1,3));
Output:
Array
(
[0] => Array
(
[0] => one
[1] => InfoOne
[2] => LibOne
)
[1] => Array
(
[0] => two
[1] => InfoTwo
[2] => LibTwo
)
[2] => Array
(
[0] => three
[1] => InfoThree
[2] => LibThree
)
)
Upvotes: 1
Reputation: 9583
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>';
Upvotes: 3