1

I am trying to separate an array into two separate arrays. For example, if I have an array like this

Array([0]=>Hello[1]=>I'm[2]=>Cam)

I want to split it into two arrays and add another string

Array1([0]=>Hello[1]=>There,)
Array2([0]=>I'm[1]=>Cam)

Then finally add the two together

Array([0]=>Hello[1]=>There,[2]=>I'm[3]=>Cam)

What would be the simplest way to do this? I know I can use array merge to put the two together but I don't know how to separate them at a certain point.

I'm also doing this on a large file that will be constantly getting bigger, so I cant use array_chunk()

  • 2
    Possible duplicate of [Insert new item in array on any position in PHP](http://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php) – Ruslan Osmanov Dec 19 '16 at 03:37

3 Answers3

1

Looking at your end result goal, I think a shorter method to get your desired response is to use array_splice which lets you insert into a middle of an array....

$arrayVarOriginal = array('Hello', 'Im', 'Cam');
$arrayVarExtra = array('There');
array_splice($arrayVarOriginal, 1, 0, $arrayVarExtra);

This should send you back Array([0]=>Hello[1]=>There,[2]=>Im[3]=>Cam) like you wanted!

The above avoids having to split up the array.

HOWEVER

If you did want to do it the hard way, here is how you would...

$arrayVarOriginal = array('Hello', 'Im', 'Cam');
$array1stPart = array(arrayVarOriginal[0], 'There');
$array2ndPart = array_shift($array1stPart);
$finalArray = array_merge($array1stPart, $array2ndPart);

How? array_shift removes the first item from any array, so that how we get $array2ndPart.... and $array1stPart is even easier as we can just manually build up a brand new array and take the first item from $arrayVarOriginal which is at position 0 and add 'There' in as our own new position 1.

Hope that helps :)

array_shift, array_splice, and array_merge are what you need to look into.

mrmrw
  • 120
  • 9
0

Based from your question, here step-by-step to get what you want for your final output.

1) Split Array

$arr = array('Hello', 'I\'m', 'Cam');
$slice = count($arr) - 1;

$arr1 = array_slice($arr, 0, -$slice);
$arr2 = array_slice($arr,1);

so, you get two new array here $arr1 and $arr2

2) Add new string

$arr1[] = "There";

3) Finally, combine the array

$arr = array_merge($arr1, $arr2)

Here sample output when you print_r the $arr

Array
(
    [0] => Hello
    [1] => There
    [2] => I'm
    [3] => Cam
)
weirdo
  • 334
  • 1
  • 10
  • This would fail to insert into the first position correctly if the original array was ever of variable length -- for instance array('Hello', 'I\'m', 'James', 'Cameron'); --- now the end result would be Hello/There/Im/James instead of Hello/There/Im/James/Cameron --- the answer I provided above, both solutions function with variable length arrays – mrmrw Dec 19 '16 at 03:48
  • 1
    i just give solution base on the question. but thanks @mrmrw. see the edit answer. Now it should accept any length of array – weirdo Dec 19 '16 at 04:04
  • Nice work! I was presuming it could be of variable lengths otherwise the OP could just make a new variable manually if it was always same lengh... i.e. $newArray=array($originalArray[0], 'There', $originalArray[1], $originalArray[2], $originalArray[3]); – mrmrw Dec 19 '16 at 04:12
0

array_slice second parameter is position where you want split. So you can do this dynamically by count the array length.

$string = array("Hello","I'm","Cam");

$firstArray = array_slice($string ,0,1);
array_push($firstArray,"There,");
$secondArray =  array_slice($string ,1,2);
echo "<pre>";
print_r($firstArray);


echo "==========="."<pre>";
print_r($secondArray);

echo "<pre>";
print_r(array_merge($firstArray,$secondArray));

//out put
Array
(
    [0] => Hello
    [1] => There,
)
===========

Array
(
    [0] => I'm
    [1] => Cam
)

Array
(
    [0] => Hello
    [1] => There,
    [2] => I'm
    [3] => Cam
)

Hope it will be worked for your requirement. If not or you need more specify regarding this you can clarify your need.

Didarul Alam
  • 79
  • 2
  • 18