1

This is what I want to do. I have an array that I then want to split up into all the elements that it has. For example I have:

$originalArray = array(1,2,3,4,5);

And I want to split this array to look like this.

$array1 = array(1);
$array2 = array(2);
$array3 = array(3);
$array4 = array(4);
$array5 = array(5);

if the original array had more elements then I would like it to split into all those arrays.

for($i = 0; $i < count($originalArray); $i++ ){

    /*this is where I am stuck, I would like it if the word "array" would be able to concat with the iterator $i to make $array1, $array2 and so on*/ 

    $array.$i = array($originalArray[$i]);


}
0

3 Answers 3

3

try this :

<?php
$input_array = array('a', 'b', 'c', 'd', 'e');

echo "<pre>";
print_r(array_chunk($input_array, 1));
?>

Ref : http://php.net/manual/en/function.array-chunk.php

Sign up to request clarification or add additional context in comments.

Comments

1
<?php
$originalArray = array(1,2,3,4,5);
$temp = array();
$i=1;
foreach ($originalArray as $elem) {
  $temp['array' . $i] = $elem;
  $i++;
}
extract($temp);
print_r($array1);echo '<br/>';
print_r($array2);echo '<br/>';
print_r($array3);echo '<br/>';
print_r($array4);echo '<br/>';
print_r($array5);echo '<br/>';

Demo

Comments

0

try this

$originalArray = array(1,2,3,4,5);

$splitArray = explode(",",$orginalArray);

for($i = 0; $i < count($originalArray); $i++ ){


    $array.$i = array($splitArray($i));


}

1 Comment

Fatal error: Function name must be a string at $array.$i = array($splitArray($i));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.