如何提取特定阵列键和值到另一个阵列? [英] How to extract specific array keys and values to another array?

查看:123
本文介绍了如何提取特定阵列键和值到另一个阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数组像这样的数组:

I have an array of arrays like so:

array( array(), array(), array(), array() );

在主阵列内的数组包含4个按键和值。键是所有类似的阵列中是相同的:

the arrays inside the main array contain 4 keys and their values. The keys are the same among all arrays like this:

array( 'id' => 'post_1',
       'desc' => 'Description 1',
       'type' => 'type1',
       'title' => 'Title'
     );

array( 'id' => 'post_2',
       'desc' => 'Description 2',
       'type' => 'type2',
       'title' => 'Title'
     );

所以,我想创建另一个数组并提取 ID 键入值,并把它们放在一个新的数组像这样的:

So I want to create another array and extract the id and type values and put them in a new array like this:

array( 'post_1' => 'type1', 'post_2' => 'type2'); // and so on

此数组中的密钥将 ID 键旧的阵列,其价值将是键入键。

The keys in this array will be the value of id key old arrays and their value will be the value of the type key.

所以,才有可能实现这一目标?我试图寻找 php.net阵列功能,但我不知道要使用哪个函数?

So is it possible to achieve this? I tried searching php.net Array Functions but I don't know which function to use?

和在此先感谢。

推荐答案

只需使用一个很好的醇'循环:

Just use a good ol' loop:

$newArray = array();
foreach ($oldArray as $entry) {
    $newArray[$entry['id']] = $entry['type'];
}

这篇关于如何提取特定阵列键和值到另一个阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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