Access data inside array in PHP

0

Hey guys, all right?

I have a problem with a project I'm developing. I get a form where the user informs the data but in this form, it is possible that it clicks a button to insert new inputs and inform other data.

The array I am using as a test is this: (But it can vary as the user enters other fields.)

array:8 [▼
    "name" => array:3 [▼
        0 => "101"
        1 => "102"
        2 => "103"
    ]
    "gate" => array:3 [▼
        0 => "1"
        1 => "1"
        2 => "1"
    ]
    "chairs_initial" => array:3 [▼
        0 => "80"
        1 => "161"
        2 => "242"
    ]
    "chairs_final" => array:3 [▼
        0 => "160"
        1 => "241"
        2 => "322"
    ]
    "tickets_avaliable" => array:3 [▼
        0 => "80"
        1 => "80"
        2 => "80"
    ]
    "price_full" => array:3 [▼
        0 => "40"
        1 => "40"
        2 => "40"
    ]
    "price_half" => array:3 [▼
        0 => "20"
        1 => "20"
        2 => "20"
    ]
    "plant_sector" => array:3 [▼
        0 => UploadedFile {#291 ▶}
        1 => UploadedFile {#303 ▶}
        2 => UploadedFile {#287 ▶}
    ]
]

And in it I travel as follows:

<?php
foreach ($temp_sectors as $i => $temp) {
    foreach ($temp as $j => $val) {
        $sec[$j] = $val;
        dd($sec); 
    }
}

And I always have the return of the first position of 'name' like this:

0 => "101"

No access to 'gate', 'chairs_initial', 'chairs_final' ... I do not know if there is another way, but what I would like to put together is to separate this data by position in a temporary array, for example:

array:0 [▼
    "name" => "101"
    "gate" => "1"
    "chairs_initial" => "80"
    ...
]
array:1 [▼
    "name" => "102"
    "gate" => "1"
    "chairs_initial" => "161"
    ...
]
array:2 [▼
    "name" => "103"
    "gate" => "1"
    "chairs_initial" => "242"
    ...
]

For me to be able to after that perform a foreach on this temporary array and insert the data into my database. I do not know if there is another simpler form, but if there is one I would like to understand it.

Thanks in advance!

    
asked by anonymous 24.09.2018 / 14:02

1 answer

2

I do not understand if the array keys are also dynamic, but if only the array elements are dynamic, you can use the array_map to merge all variables into just one array element.

Ex:

<?php 

$input = [
    "name" => ["101", "102", "103"],
    "gate" => ["1", "1", "1"],
    "chairs_initial" => ["80", "161", "242"],
    "chairs_final" => ["160", "241", "322"],
];

$temp = array_map(function ($name, $gate, $ch_init, $ch_final) {
    return [
        "name" => $name,
        "gate" => $gate,
        "chairs_initial" => $ch_init,
        "chairs_final" => $ch_final,
    ];
}, $input['name'], $input['gate'], $input['chairs_initial'], $input['chairs_final']);

var_export($temp);

You're going to start:

array (
    0 => 
    array (
        'name' => '101',
        'gate' => '1',
        'chairs_initial' => '80',
        'chairs_final' => '160',
    ),
    1 => 
    array (
        'name' => '102',
        'gate' => '1',
        'chairs_initial' => '161',
        'chairs_final' => '241',
    ),
    2 => 
    array (
        'name' => '103',
        'gate' => '1',
        'chairs_initial' => '242',
        'chairs_final' => '322',
    ),
)

Code working ...

    
24.09.2018 / 14:47