[ 0 => 562, 1 => 570, 2 => 574 ]
is the same as
[562, 570, 574]
If you have an associative array with consecutive integer keys starting at 0, then you have a normal array
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello all!
I am attempting to use the detach() method on a many-to-many pivot table and having trouble creating an array of IDs to send to the method.
From the Laravel 5.8 documentation:
For convenience, attach and detach also accept arrays of IDs as input:
$user = App\User::find(1);
$user->roles()->detach([1, 2, 3]);
My HTML form is sending the correct array of checkbox values as the following:
array:1 [ "input" => array:3 [ 0 => "562" 1 => "570" 2 => "574" ] ]
My question is, how do I make a new array from the values of my form input to send to the detach() method?I have tried all kinds of loops and array functions but always end up with a new array with key=>value pairs. According to the documentation, I need an array that looks like this:
detach([562,570,574]);
[ 0 => 562, 1 => 570, 2 => 574 ]
is the same as
[562, 570, 574]
If you have an associative array with consecutive integer keys starting at 0, then you have a normal array
Please or to participate in this conversation.