PHP如何将值从一个数组传递到另一个数组? [英] PHP How can I pass values from one array to another?

查看:209
本文介绍了PHP如何将值从一个数组传递到另一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Options数组中传递一些值,并将它们放入一个名为$ theDefaults的新数组中。

I'm trying to pass some of the values from theOptions array and drop them into a new array called $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数组应如下所示:

So, $theDefaults array should look like this:

$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;
}

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

It returns this. 0 :Title 11 :Attribute 12 :Title 23 :Attribute 24 :Title 35 :Attribute 3

看起来太近了,但是为什么其中的数字

Looks to be soooo close, but why are the numbers in the array?

推荐答案

比这还简单:

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

这篇关于PHP如何将值从一个数组传递到另一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆