PHP如何将值从一个数组传递到另一个数组?
问题描述:
我正在尝试从Options数组中传递一些值,并将它们放入一个名为$ theDefaults的新数组中。
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')
);
因此,$ theDefaults数组应如下所示:
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;
}
它将返回此值。
0:标题11:属性12:标题23:属性24:标题35:属性3
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?
答
比这还简单:
$theDefaults = array();
foreach($theOptions as $v) {
$theDefaults[$v['title']] = $v['attribute'];
}