4

I have an array that start with a specific index, always will be 43 the first index, after that I have info and I would like to split it into 2 after find the value products but not always the value products will be the index 64 it will be different everytime.

So I tried this to find the index where the value products is

$index = array_search('products', $data);

This way I can find the index where I would like to split the array in 2, but I have no idea how, I tried with array_chunk but I couldn't make it work

This is my original array:

Array
(
    [43] => 1234
    [44] => 001
    [45] => 100
    [46] => 0
    [47] => 0
    [48] => 2
    [49] => 
    [50] => 1234
    [51] => 001
    [52] => 100
    [53] => 0
    [54] => 0
    [55] => 2
    [56] => 
    [57] => 
    [58] => 
    [59] => 
    [60] => 
    [61] => 0
    [62] => 1
    [63] => 
    [64] => products
    [65] => 1234
    [66] => 001
    [67] => 100
    [68] => 0
    [69] => 0
    [70] => 2
    [71] => 
    [72] => 1234
    [73] => 001
    [74] => 100
    [75] => 0
    [76] => 0
    [77] => 2
    [78] => 
    [79] => 
    [80] => 
    [81] => 
    [82] => 
    [83] => 0
    [84] => 1
)

And I would like something like this:

Array
(
    [43] => 1234
    [44] => 001
    [45] => 100
    [46] => 0
    [47] => 0
    [48] => 2
    [49] => 
    [50] => 1234
    [51] => 001
    [52] => 100
    [53] => 0
    [54] => 0
    [55] => 2
    [56] => 
    [57] => 
    [58] => 
    [59] => 
    [60] => 
    [61] => 0
    [62] => 1
    [63] =>
)

Array
( 
    [65] => 1234
    [66] => 001
    [67] => 100
    [68] => 0
    [69] => 0
    [70] => 2
    [71] => 
    [72] => 1234
    [73] => 001
    [74] => 100
    [75] => 0
    [76] => 0
    [77] => 2
    [78] => 
    [79] => 
    [80] => 
    [81] => 
    [82] => 
    [83] => 0
    [84] => 1
)

Also without the index of the value products, do you have some idea what function can I use to achieve this or another way to do it? I hope you can help me, thank you.

3
  • 1
    have you tried array_slice? Commented Jan 3, 2017 at 0:56
  • Just to get the index where is the value products Commented Jan 3, 2017 at 0:58
  • 1
    @Chris I think I found the solution with your comment :) Commented Jan 3, 2017 at 1:01

3 Answers 3

6

Almost, array_chunk() makes multiple pieces instead you want to slice the array in two pieces by a given key, this can be done with array_slice()

$split = array_search('products', $data);

print_r(array_slice($data, 0, $split))   // first part
print_r(array_slice($data, $split + 1)); // second part
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer :)
FYI: this will remove 'products' from the final result. If you just want to split into two arrays at a certain index, use $split instead of $split + 1
2
// an array
$array = array(1, 2, 3, "products", 4, 5);

// $offset = 3
$offset = array_search("products", $array);

// array(4,5)
$last_batch = array_slice($array, ($offset + 1));

// array(1,2,3)
$first_batch = array_slice($array, 0, $offset);

Comments

1

Maybe this helps :)

$Array = array (10, 20, 30, 40);
$count = 0;
foreach($Array as $my_array)
{
 if($my_array == "products")
 {
   count++
   array_chunk($Array, count);
 }
}

Comments

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.