Access data within PHP array

Asked

Viewed 98 times

0

Hey, guys, all right?

I have a problem with a project I’m working on. I receive a form where the user informs the data but in this form, it is possible that he click a button to insert new inputs and inform other data.

The array I’m testing is this: (But it may 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 go the following way:

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

And I always get the return of the first position of the name so:

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 mount is to separate this data according to the position in a temporary array, 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"
    ...
]

So I can after that perform a foreach on this temporary array and insert the data into my database. I don’t know if there’s another simpler way, but if there is I’d like to understand it.

From now on, thank you!

1 answer

2


I didn’t understand if the array keys are also dynamic, but if only the number of array elements are dynamic you can use the method array_map to merge all variables into only one element of the array.

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);

Print:

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...

  • The keys are not dynamic, it was just that! Haha. I didn’t remember the array_map. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.