I'm trying to pass some of the values from theOptions array and drop them into a new array called $theDefaults.
$theOptions = array(
'item1' => array('title'=>'Title 1','attribute'=>'Attribute 1','thing'=>'Thing 1'),
'item2' => array('title'=>'Title 2','attribute'=>'Attribute 2','thing'=>'Thing 2'),
'item3' => array('title'=>'Title 3','attribute'=>'Attribute 3','thing'=>'Thing 3')
);
So, $theDefaults array should look like this:
$theDefaults = array(
'Title 1' => 'Attribute 1',
'Title 2' => 'Attribute 2',
'Title 3' => 'Attribute 3'
);
However, I cannot figure out how to do this. Have tried this but it is clearly not quite working.
$theDefaults = array(); foreach($theOptions as $k=>$v) { array_push($theDefaults, $v['title'], $v['attribute']); }
but when I run this...
foreach($theDefaults as $k=>$v) {
echo $k .' :'.$v;
}
It returns this. 0 :Title 11 :Attribute 12 :Title 23 :Attribute 24 :Title 35 :Attribute 3
Looks to be soooo close, but why are the numbers in the array?
It's even simpler than that:
$theDefaults = array();
foreach($theOptions as $v) {
$theDefaults[$v['title']] = $v['attribute'];
}
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.