L
L
Li2016-06-24 00:41:26
Laravel
Li, 2016-06-24 00:41:26

Editing a multidimensional array?

Good night everybody. The second day I struggle with the task.
There is a dynamic form
Fields are added dynamically, for clarity below I have indicated a diagram of a possible form

${id} - $cinema->id
<form method="POST">
    @foreach($cinemas as $cinema)
        Кинотеатр
        @foreach(сеансы)
             <input type="text" name="cinema[${id}][date_start][]">
             <input type="text" name="cinema[${id}][price][]">
         @endforeach
    @endforeach
    <input type="submit">
</form>

It is necessary to add the cinema_id key to the array
In order to understand how many sessions and to which cinema it is necessary to add them.
As a result, I need to get something like this:
0 => [
    'cinema_id' => 24,
    'date_start' => 0,
    'price' => 100,
],
1 => [
    'cinema_id' => 23,
    'date_start' => 0,
    'price' => 150,
],
2 => [
    'cinema_id' => 23,
    'date_start' => 0,
    'price' => 140,
]

After that, using the cycle, I will add 3 sessions, with the specified parameters.
Database structure:
movie_id | cinema_id | date_start | price
If anyone has encountered a similar problem or knows the solution to the problem, please help, thank you in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-06-24
@cubaPro

$cinemas = $request->cinema;
$cinemas = collect($cinemas)->map(function($item, $key){
    $item['cinema_id'] = $key;
    return $item;
})->toArray();
dd($cinemas);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question