0

I have this array:

[
    [
        "302260",
        "302250",
        "302248",
    ],
    [
        "Bruce Willis",
        "Jackie Chan",
        "Gary Oldman",
    ],
]

I need insert multiple this values, I found this answer: How to insert multiple rows from a single query using eloquent/fluent, but I don't know how I can add column name to my array, to make it look like this:

[
    [
        "user_id" => "302260",
        "user_id" => "302250",
        "user_id" => "302248",
    ],
    [
        "name" => "Bruce Willis",
        "name" => "Jackie Chan",
        "name" => "Gary Oldman",
    ]
]

I can't use array_map, because I have different values in the key: 1, 2, 3

Ethan
  • 4,295
  • 4
  • 25
  • 44
michal
  • 1,534
  • 5
  • 28
  • 62

1 Answers1

2

An array key needs to be unique within an array. e.g. you can't have multiple keys called user_id within a single array. What you can do instead is have an array in the following format:

Array
(
    [1] => Array
        (
            [user_id] => 1
            [name] => User1
        )

    [2] => Array
        (
            [user_id] => 2
            [name] => User2
        )
)

That's also what the answer you're linking to describes. The php code for the above array looks like this:

$users = array(
  1 => array("user_id" => 1, "name" => "User1"),
  2 => array("user_id" => 2, "name" => "User2"),
);
clem
  • 3,345
  • 1
  • 22
  • 33
  • Thanks for the answer, I convert my array, but now I don't know how i can change key `1,2,3` to name of column `user_id` and second array `name`: http://iv.pl/images/50813748968996167864.png I need have this: http://iv.pl/images/46100388856337307172.png – michal Oct 07 '17 at 16:39
  • 1
    the array in the second picture doesn't work as the keys aren't unique. if you want named keys you'll have to use the format I showed you in the second example. you can then use a loop to get the name and if of each user. – clem Oct 07 '17 at 22:20