如何将特定的数组键和值提取到另一个数组? [英] How to extract specific array keys and values to another array?
问题描述
我有一个像这样的数组:
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
和 type
值并将它们放入一个新数组中,如下所示:
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屋!