I have an array
$arr = array(
1=>'xyz',
2=>'abc',
3=>'pqr'
);
I want to convert this to
$multiarr=array(
[0]=>array(
['id']=>1,
['name']=>'abc'),
[1]=>array(
['id']=>2,
['name']=>'xyz'),
[2]=>array(
['id']=>3,
['name']=>'pqr')
);
id is key and the name is the value of the first array
how can I implement this optimistically
I have done this
$keys=array_keys($arr);
$values=array_values($arr) ;
$multiarr=array();
for($i=0; $i<count($keys); $i++)
{
$multiarr[$i]['id']=$keys[$i];
$multiarr[$i]['name']=$values[$i];
}
Thanks.
Should really be trying this yourself mate, but this should help:
$arr = array(
1=>'xyz',
2=>'abc',
3=>'pqr'
);
$MultiArr = array();
$i = 0;
foreach($arr as $ID=>$Name){
$MultiArr[$i]['id'] = $ID;
$MultiArr[$i]['name'] = $Name;
$i++;
}
print_r($MultiArr);
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.