Sta*_*rfs 0 php arrays dynamically-generated
我正在尝试传递theOptions数组中的一些值,并将它们放入一个名为$ 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')
);
Run Code Online (Sandbox Code Playgroud)
所以,$ theDefaults数组应如下所示:
$theDefaults = array(
'Title 1' => 'Attribute 1',
'Title 2' => 'Attribute 2',
'Title 3' => 'Attribute 3'
);
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何做到这一点.试过这个,但显然不太合适.
Run Code Online (Sandbox Code Playgroud)$theDefaults = array(); foreach($theOptions as $k=>$v) { array_push($theDefaults, $v['title'], $v['attribute']); }
但是当我跑这个......
foreach($theDefaults as $k=>$v) {
echo $k .' :'.$v;
}
Run Code Online (Sandbox Code Playgroud)
它返回这个.0:标题11:属性12:标题23:属性24:标题35:属性3
看起来太近了,但为什么数组中的数字?
它甚至比那简单:
$theDefaults = array();
foreach($theOptions as $v) {
$theDefaults[$v['title']] = $v['attribute'];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3572 次 |
| 最近记录: |