mbkazi
mbkazi

Reputation: 49

multiDimensional Array need as

Actual array as below is basically the array of $_POST. One can understand what is coming from the form. three controls with same name different value. What i need is below this array.

Array
(
    [mytext] => Array
        (
            [0] => aaaa
            [1] => bbbb
            [2] => ddd
        )

    [mysel] => Array
        (
            [0] => one
            [1] => two
            [2] => two
        )

    [submit] => Submit
)

I need the array in row format below but be dynamic based of $_POST data.

Array
(
    [0] => Array
        (
            [0] => aaaa
            [1] => one

        )

    [1] => Array
        (
            [0] => bbbb
            [1] => two

        )

    [2] => Array
        (
            [0] => dddd
            [1] => two

        )


)

Upvotes: 0

Views: 74

Answers (3)

mbkazi
mbkazi

Reputation: 49

// Code To Get controls value in row wise
$count=0;
foreach($_POST as  $key=>$val){
    foreach($_POST[$key] as $val2){
        $row[$count][]=$val2;
        $count++;
    }
    $count=0;
}
print_r($row);

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227200

Loop through one of the fields, and then pull the corresponding value from the other field.

$result = array();
foreach($_POST['mytext'] as $k=>$v){
    $result[] = array($v, $_POST['mysel'][$k]);
}

You can also use array_map to do this:

// PHP 5.3+
$result = array_map(function($a, $b){
    return array($a, $b);
}, $_POST['mytext'], $_POST['mysel']);

// PHP <= 5.2
$result = array_map(create_function('$a,$b', 'return array($a,$b);'), $_POST['mytext'], $_POST['mysel']);

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Try this:

$out = Array();
foreach($_POST['mytext'] as $k=>$v) {
    $out[$k] = Array($v,$_POST['mysel'][$k]);
}
var_dump($out);

Upvotes: 1

Related Questions