如何将特定的数组键和值提取到另一个数组中?

时间:2021-12-20 01:04:04

I have an array of arrays like so:

我有一个数组,如下所示:

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

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

主数组中的数组包含4个键及其值。所有数组中的键都是相同的:

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

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

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

因此我想创建另一个数组,提取id和类型值,并将它们放入一个新的数组中,如下所示:

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

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.

这个数组中的键将是id键旧数组的值,它们的值将是类型键的值。

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

那么,是否有可能实现这一目标呢?我尝试搜索php。net数组函数,但是我不知道使用哪个函数?

And thanks in advance.

提前和感谢。

3 个解决方案

#1


27  

Just use a good ol' loop:

使用一个好的ol循环:

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

#2


75  

PHP recently added new function to their array functions that does exactly what you wanted. I'm answering this in hopes that it may help someone in future with this question.

PHP最近向它们的数组函数添加了新函数,这正是您想要的。我回答这个问题是希望它能在将来帮助别人解决这个问题。

The function that does this is array_column. To get what you wanted you would write:

这个函数是array_column。为了得到你想要的,你可以这样写:

array_column($oldArray, 'type', 'id');

Please note that it was only introduced in PHP 5.5.0 version, so to use it on lower versions of PHP either use accepted answer or take a look at how this function was implemented in PHP and use this library: https://github.com/ramsey/array_column

请注意,它只是在PHP 5.5.0版本中引入的,所以要在较低版本的PHP中使用它,要么使用已接受的答案,要么看看这个函数是如何在PHP中实现的,然后使用这个库:https://github.com/ramsey/array_column

#3


-1  

Let's suppose we have an array $oldArray from which we want to extract and second array $array is one in which we will be storing the extracted values which is initially blank.

假设我们有一个数组$oldArray我们想从中提取第二个数组$array我们将在其中存储最初为空的提取值。

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

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

$array = array(); //Blank array

foreach(oldArray as $i => $oldarray){
    $array[$i][$oldarray['id']] = $oldarray['type'];
}

Now $array have key/value pair as below

现在,$array有如下所示的键/值对

array(
    0 => array('post_1' => 'type1'),

    1 => array( 'post_2' => 'post_2')
);

#1


27  

Just use a good ol' loop:

使用一个好的ol循环:

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

#2


75  

PHP recently added new function to their array functions that does exactly what you wanted. I'm answering this in hopes that it may help someone in future with this question.

PHP最近向它们的数组函数添加了新函数,这正是您想要的。我回答这个问题是希望它能在将来帮助别人解决这个问题。

The function that does this is array_column. To get what you wanted you would write:

这个函数是array_column。为了得到你想要的,你可以这样写:

array_column($oldArray, 'type', 'id');

Please note that it was only introduced in PHP 5.5.0 version, so to use it on lower versions of PHP either use accepted answer or take a look at how this function was implemented in PHP and use this library: https://github.com/ramsey/array_column

请注意,它只是在PHP 5.5.0版本中引入的,所以要在较低版本的PHP中使用它,要么使用已接受的答案,要么看看这个函数是如何在PHP中实现的,然后使用这个库:https://github.com/ramsey/array_column

#3


-1  

Let's suppose we have an array $oldArray from which we want to extract and second array $array is one in which we will be storing the extracted values which is initially blank.

假设我们有一个数组$oldArray我们想从中提取第二个数组$array我们将在其中存储最初为空的提取值。

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

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

$array = array(); //Blank array

foreach(oldArray as $i => $oldarray){
    $array[$i][$oldarray['id']] = $oldarray['type'];
}

Now $array have key/value pair as below

现在,$array有如下所示的键/值对

array(
    0 => array('post_1' => 'type1'),

    1 => array( 'post_2' => 'post_2')
);