简体   繁体   中英

is it possible to convert associative array to multidimensional array?

I have a form fields coming in the following format.

echo '<pre>';
print_r($_REQUEST);
echo '</pre>';exit;

[customer_id] = [0=> 4,1=>5];
[hobies]=>[0=> circket,1=>chess];

Here i want to convert into multidimensional associative array.

$output  = [
4=>[
'hobies'=>'circket'
],
5=>[
'hobies'=> 'chess'
]
];
<?php
$customer_id = array(4, 5);
$hobies = array('circket', 'chess');
$output = array();
foreach($customer_id as $index=>$cid){
    $output[$customer_id[$index]] = array("hobies"=>$hobies[$index]);
}
var_dump($output);

Output is

array(2) {
  [4]=>
  array(1) {
    ["hobies"]=>
    string(7) "circket"
  }
  [5]=>
  array(1) {
    ["hobies"]=>
    string(5) "chess"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM