How can I reorganize this array? [duplicate]

-2

I have the following array:

$array_atual = array (

    'id_item' => array('1', '3'), // 'id_item', 'id_item'
    'cantidad' => array('15', '10') // 'cantidad', 'cantidad'
);

I would like to leave it as follows:

$array_final (

    '0' => array('1', '15'); // 'id_item', 'cantidad'
    '1' => array('3', '10')     // 'id_item', 'cantidad'
);

How can I make this change?

    
asked by anonymous 10.06.2018 / 12:44

1 answer

1

Given that the keys always match the array id_item and cantidad , you can do this in the following way:

$array_final = array();
$array_atual = array (

    'id_item' => array('1', '3'), // 'id_item', 'id_item'
    'cantidad' => array('15', '10') // 'cantidad', 'cantidad'
);

foreach($array_atual['id_item'] as $key => $value){
     $newArray = array($value, $array_atual['cantidad'][$key]);
     array_push($array_final, $newArray);
}

Example on Ideone

    
10.06.2018 / 13:35