0

I want to divided my $request->all() array into two arrays so I can send both arrays into a different database table.

This is what I receive:

  array:12 [▼
  "key1" => "value1"
  "key2" => "value2"
  "key3" => "value3"
  "key4" => "value4"
  "key5" => "value5"
  "key6" => "value6"
  "key7" => "value7"
  "key8" => "value8"
  "key9" => "value9"
  "key10" => "value10"
  "_token" => "47p7eZpSOVOP0kSrL1HBSXn2OrvYT1kCiNoR2Ekr"
  "submit" => "Save"
  ]

But I want two different arrays, like so:

  array:5 [▼
  "key1" => "value1"
  "key2" => "value2"
  "key3" => "value3"
  "key4" => "value4"
  "key5" => "value5"
  ]

And:

array:7 [▼
  "key6" => "value6"
  "key7" => "value7"
  "key8" => "value8"
  "key9" => "value9"
  "key10" => "value10"
  "_token" => "47p7eZpSOVOP0kSrL1HBSXn2OrvYT1kCiNoR2Ekr"
  "submit" => "Save"
  ]

How can I achieve this? I already had something in mind for the first array but I don't know how to get the second one. This is what I have:

$first_array = [];
$second_array = [];

foreach ($request->all() as $key => $value) {
    $first_array[$key] = $value;

    if ($key == 'key6') {
        break;
    }
}

Mark_
  • 113
  • 1
  • 12
  • [`array_chunk()`](https://www.php.net/manual/en/function.array-chunk.php) A simple google would have found you it – RiggsFolly Dec 06 '21 at 16:42
  • Also, as you're using Laravel, if you have a `Collection` instead of a PHP Array, there's the `chunk` method: https://laravel.com/docs/8.x/collections#method-chunk – Tim Lewis Dec 06 '21 at 16:43
  • Chunk doesn't do the trick because the first array has to be 5 values and the second array can be 20 values. – Mark_ Dec 06 '21 at 17:15
  • I mean, if you had included those details in your post somewhere, we would have given you better suggestions. See the `array_slice()` answer below, or `splice` for Collections: https://laravel.com/docs/8.x/collections#method-splice – Tim Lewis Dec 06 '21 at 17:31

2 Answers2

2

You can use array_slice() as you say you want to split the array into the first 5 and then all the rest.

$input = [  "key1" => "value1",
            "key2" => "value2",
            "key3" => "value3",
            "key4" => "value4",
            "key5" => "value5",
            "key6" => "value6",
            "key7" => "value7",
            "key8" => "value8",
            "key9" => "value9",
            "key10" => "value10",
            "_token" => "47p7eZpSOVOP0kSrL1HBSXn2OrvYT1kCiNoR2Ekr",
            "submit" => "Save"
];

$p1 = array_slice($input, 0, 5);

$p2 = array_slice($input, 5); 

print_r($p1);
print_r($p2);

RESULTS

Array
(
    [key1] => value1
    [key2] => value2
    [key3] => value3
    [key4] => value4
    [key5] => value5
)
Array
(
    [key6] => value6
    [key7] => value7
    [key8] => value8
    [key9] => value9
    [key10] => value10
    [_token] => 47p7eZpSOVOP0kSrL1HBSXn2OrvYT1kCiNoR2Ekr
    [submit] => Save
)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
1

Array chunk doesnt do the trick but this one works:

$first_array = [];
$second_array = [];
$count = 0;

foreach ($request->all() as $key => $value) {
    if ($count <= 7) {
        $first_array[$key] = $value;
    } else {
        $second_array[$key] = $value;
    }

    $count++;
}

dd($first_array);

Mark_
  • 113
  • 1
  • 12