PHP如何将值从一个数组传递到另一个数组?
我试图从options数组中传递一些值,并将它们放入一个名为$theDefaults的新数组中PHP如何将值从一个数组传递到另一个数组?,php,arrays,dynamically-generated,Php,Arrays,Dynamically Generated,我试图从options数组中传递一些值,并将它们放入一个名为$theDefaults的新数组中 $theOptions = array( 'item1' => array('title'=>'Title 1','attribute'=>'Attribute 1','thing'=>'Thing 1'), 'item2' => array('title'=>'Title 2','attribute'=>'Attribute 2','thi
$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数组应该如下所示:
$theDefaults = array(
'Title 1' => 'Attribute 1',
'Title 2' => 'Attribute 2',
'Title 3' => 'Attribute 3'
);
然而,我不知道如何做到这一点。
我已经试过了,但显然不太管用
但是当我运行这个
foreach($theDefaults as $k=>$v) {
echo $k .' :'.$v;
}
它返回这个。
0:标题11:属性12:标题23:属性24:标题35:属性3
看起来很接近,但为什么数组中的数字是?比这更简单:
$theDefaults = array();
foreach($theOptions as $v) {
$theDefaults[$v['title']] = $v['attribute'];
}
比这更简单:
$theDefaults = array();
foreach($theOptions as $v) {
$theDefaults[$v['title']] = $v['attribute'];
}
注意,问题是
array\u push
将第一个参数之后的所有参数都推到数组的末尾。哎哟,比我快几秒钟。:)哇!太快了。谢谢成功了。显然,我可以在8分钟内宣布你的答案是正确的。注意,问题是array\u push
会将第一个参数之后的所有参数都推到数组的末尾。哎哟,比我快几秒钟。:)哇!太快了。谢谢成功了。显然我可以在8分钟内宣布你的答案是正确的。