簡體   English   中英

PHP如何將值從一個數組傳遞到另一個數組?

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

我正在嘗試從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')

);

因此,$ 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

看起來太近了,但是為什么數組中的數字呢?

比這更簡單:

$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