2013-03-29 79 views
0

我想从选项数组中传递一些值,并将它们放入一个名为$ theDefaults的新数组中。PHP我怎样才能从一个数组传递值到另一个?

$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' 

); 

但是,我想不出如何做到这一点。 已经尝试过,但显然不是很有效。

$theDefaults = array(); 

foreach($theOptions as $k=>$v) { 
    array_push($theDefaults, $v['title'], $v['attribute']); 
} 

但是当我运行这个...

foreach($theDefaults as $k=>$v) { 
    echo $k .' :'.$v; 
} 

它返回。 0:标题11:属性12:标题23:属性24:标题35:属性3

看起来是soooo close,但为什么数组中的数字?

回答

6

它比这更简单:

$theDefaults = array(); 
foreach($theOptions as $v) { 
    $theDefaults[$v['title']] = $v['attribute']; 
} 
+0

注意到的问题是,'array_push'第一到数组结束后推动所有参数。 – castis

+0

Oooops,打我几秒钟。 :) –

+0

哇。那很快。谢谢!有效。显然我可以在8分钟内宣布你的答案是正确的。 – Starfs

相关问题