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

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

问题描述

我有一个像这样的数组:

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'
     );

所以我想创建另一个数组并提取 idtype 值并将它们放入一个新数组中,如下所示:

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键旧数组的值,它们的值将是type键的值.

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 Array Functions 但我不知道要使用哪个函数用吗?

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天全站免登陆