简体   繁体   English

PHP如何将值从一个数组传递到另一个数组?

[英]PHP How can I pass values from one array to another?

I'm trying to pass some of the values from theOptions array and drop them into a new array called $theDefaults. 我正在尝试从Options数组中传递一些值,并将它们放入名为$ 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数组应如下所示:

$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 0:标题11:属性12:标题23:属性24:标题35:属性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']; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM