C. Brownsey
C. Brownsey

Reputation: 29

How to cut array into two

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()

Upvotes: 1

Views: 57

Answers (3)

Didarul Alam
Didarul Alam

Reputation: 79

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.

Upvotes: 0

weirdo
weirdo

Reputation: 334

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
)

Upvotes: 0

mrmrw
mrmrw

Reputation: 120

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.

Upvotes: 1

Related Questions