0

Input post :

$_POST['dateSlot']
$_POST['timeStart']
$_POST['timeEnd']
$_POST['quota']

These input post will resulting the below array.

Array
(
    [dateSlot] => Array
        (
            [0] => 2018-04-05
            [1] => 2018-04-05
            [2] => 2018-04-05
        )

    [timeStart] => Array
        (
            [0] => 11:06 AM
            [1] => 10:06 AM
            [2] => 9:06 AM
        )

    [timeEnd] => Array
        (
            [0] => 11:06 AM
            [1] => 9:06 AM
            [2] => 7:06 AM
        )

    [quota] => Array
        (
            [0] => 12
            [1] => 10
            [2] => 10
        )
)

I'm trying to foreach them to match the index key and form another array with this idea. Not so sure if can get the value I want :

foreach ($_POST['dateSlot'] as $k => $val) {
    foreach ($_POST['timeStart'] as $k2 => $val2) {
        foreach ($_POST['timeEnd'] as $k3 => $val3) {
            foreach ($_POST['quota'] as $k4 => $val4) {
                if($k == $k2 && $k == $k3 && $k == $k4){
                    $timeslots[$k]['date_slot'] = $val;
                    $timeslots[$k]['time_start'] = $val2;
                    $timeslots[$k]['time_end'] = $val3;
                    $timeslots[$k]['event_quota'] = $val4;
                }
            }
        }
    }
}

By that foreach, I'm getting the error Illegal string offset for date_slot, time_start, time_end, and event_quota

Based on the rows in the array, my goal is to re-form the array so that they all will be combined together to form 3 rows.

Example :

Array
(
    [0] => Array
        (
            [date_slot]   => 2018-04-05
            [time_start]  => 11:06 AM
            [time_end]    => 11:06 AM
            [event_quota] => 12
        )

    [1] => Array
        (
            [date_slot]   => 2018-04-05
            [time_start]  => 10:06 AM
            [time_end]    => 9:06 AM
            [event_quota] => 10
        )

    [2] => Array
        (
            [date_slot]   => 2018-04-05
            [time_start]  => 9:06 AM
            [time_end]    => 7:06 AM
            [event_quota] => 10
        )
)
5
  • Do your input field names look like this? name="dateSlot[]" it would be better if they looked like this name="row[0][dateSlot]"where 0 is how you group each data set. Commented Apr 5, 2018 at 3:41
  • Yes my input field is like name="dateSlot[]" Commented Apr 5, 2018 at 3:42
  • Will your form have any checkbox inputs? if so I'd strongly suggest not using that notation, you will run into trouble as an unchecked checkbox won't return a presence in the post data, and your array alignment will be inconsistent. Commented Apr 5, 2018 at 4:03
  • 1
    All my input control using the text. No checkbox Commented Apr 5, 2018 at 4:10
  • Perfect, you won't run into any issues then. Commented Apr 5, 2018 at 4:11

4 Answers 4

3

Another approach to grouping this kind of data without needing to know the key names in advance.

This works by using the first row's data current( $data ) as the main iterator, then builds an array by combining the outer keys array_keys( $data ) and the inner column value array_column( $data, $column ) with array_combine() which combines two arrays of keys and an array of value to make each row's final array structure keyed by column name.

This is absolutely reliant on each multidimensional array having the same count of elements. As such this is not suitable for forms with checkbox inputs in them. At which point I would suggest using name="row[0][ColumnName]" as your name attribute and negating the need for this array processing.


$data = array(
  'Column-1'=>array('Row-1a','Row-2a','Row-3a'),
  'Column-2'=>array('Row-1b','Row-2b','Row-3b'),
  'Column-3'=>array('Row-1c','Row-2c','Row-3c')
);

$array = array();

foreach( array_keys( current( $data ) ) as $column )
{
  $array[] = array_combine( array_keys( $data ), array_column( $data, $column ) );
}

print_r( $array );

Produces

Array
(
    [0] => Array
        (
            [Column-1] => Row-1a
            [Column-2] => Row-1b
            [Column-3] => Row-1c
        )

    [1] => Array
        (
            [Column-1] => Row-2a
            [Column-2] => Row-2b
            [Column-3] => Row-2c
        )

    [2] => Array
        (
            [Column-1] => Row-3a
            [Column-2] => Row-3b
            [Column-3] => Row-3c
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

array_combine teamed up with array_column is the powerhouse of this approach
1

If you know that the element keys in all 4 of those post variables will always correlate to one timeslot element, then I think this will work for you:

foreach ($_POST['dateSlot'] as $key => $value) {
    $timeslots[$key] = [
        'date_slot'   => $_POST['dateSlot'][$key],
        'time_start'  => $_POST['timeStart'][$key],
        'time_end'    => $_POST['timeEnd'][$key],
        'event_quota' => $_POST['quota'][$key],
    ];
}

print_r($timeslots);

Comments

0
$dateSlot = $_POST['dateSlot']
$timeStart = $_POST['timeStart']
$timeEnd = $_POST['timeEnd']
$quota = $_POST['quota']

$all = array();

foreach($dateSlot as $key => $date) {
    $all[] = array(
       "data_slot" => $dateSlot[$key],
       "time_start" => $timeStart[$key],
       "time_end" => $timeEnd[$key],
       "quota"    => $quota[$key]
    ) 
}

Comments

0

Input

$array = array(
            'dateSlot' => array('2018-04-05','2018-04-05','2018-04-05'),
            'timeStart' => array('11:06 AM','10:06 AM','9:06 AM'),
            'timeEnd' => array('11:06 AM','9:06 AM','7:06 AM'),
            'quota' => array(12,10,10)
        );

Solution

$new = array();
for($i=0;$i<count($array['dateSlot']);$i++){
    $new[] = array(
        'dateSlot' => $array['dateSlot'][$i],
        'timeStart' => $array['timeStart'][$i],
        'timeEnd' => $array['timeEnd'][$i],
        'event_quota' => $array['quota'][$i],
    );
}

echo "<pre>";print_r($new);

Output

Array
(
    [0] => Array
        (
            [dateSlot] => 2018-04-05
            [timeStart] => 11:06 AM
            [timeEnd] => 11:06 AM
            [event_quota] => 12
        )

    [1] => Array
        (
            [dateSlot] => 2018-04-05
            [timeStart] => 10:06 AM
            [timeEnd] => 9:06 AM
            [event_quota] => 10
        )

    [2] => Array
        (
            [dateSlot] => 2018-04-05
            [timeStart] => 9:06 AM
            [timeEnd] => 7:06 AM
            [event_quota] => 10
        )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.