PHP将数组拆分为两个数组 - 键数组和值数组

时间:2021-02-02 12:12:28

Is there an easy way to split an array into two arrays, one consisting of all the keys and the other consisting of all the values? This would be a reverse to the action of array_combine. Is there an inbuilt function for doing such a task? Let's use an example array:

有没有一种简单的方法将数组拆分成两个数组,一个由所有键组成,另一个由所有值组成?这与array_combine的操作相反。是否有内置功能来执行此类任务?让我们使用一个示例数组:

$array = array('Tiger' => 'Forest', 'Hippo' => 'River', 'Bird' => 'Sky');

Is there a function that will split the above array into:

是否有一个函数将上面的数组拆分为:

$array_keys = array('Tiger', 'Hippo', 'Bird');
$array_values = array('Forest', 'River', 'Sky');

6 个解决方案

#1


28  

There are two functions called array_keys and array_values:

有两个函数叫array_keys和array_values:

$array_keys = array_keys($array);
$array_values = array_values($array);

#2


7  

There are two functions actually:

实际上有两个功能:

$keys = array_keys($array);
$values = array_values($array);

You can also do the exact opposite:

你也可以这样做:

$array = array_combine($keys, $values);

#3


3  

use array_keys and array_values

使用array_keys和array_values

#4


2  

Unfortunately there is no built-in inverse of array_combine. There is also no way to define one, since array_combine expects multiple parameters and we can't return multiple values from a function.

不幸的是,没有array_combine的内置逆。也没有办法定义一个,因为array_combine需要多个参数,我们不能从函数返回多个值。

We can construct an alternative to array_combine which takes a single argument: the array of keys and the array of values wrapped up together in another array. This transformation is called "uncurrying" and is performed by the "call_user_func_array" function:

我们可以构造array_combine的替代方法,它接受一个参数:键数组和在另一个数组中包含在一起的值数组。此转换称为“uncurrying”,由“call_user_func_array”函数执行:

$array_comb  = function($arr) { return call_user_func_array('array_combine', $arr); };

This alternative function does have an inverse:

这个替代函数确实有一个逆:

$array_split = function($arr) { return array(array_keys($arr), array_values($arr)); };

If we define function composition:

如果我们定义函数组合:

$compose  = function($f, $g) {
    return function($x) use ($f, $g) { return $f($g($x)); };
};

Then the following functions are all (extensionally) equal, ie. they all return their argument unchanged:

然后,以下函数全部(扩展)相等,即。他们都不改变他们的论点:

$identity      = function($x) { return $x; };
$left_inverse  = $compose($array_split, $array_comb);  // Split then combine
$right_inverse = $compose($array_comb, $array_split);  // Combine then split

Note that they accept different argument types though:

请注意,它们接受不同的参数类型:

  • $identity will work on anything.
  • $ identity将适用于任何事情。
  • $left_inverse will work on any array.
  • $ left_inverse可以在任何数组上运行。
  • $right_inverse will work on arrays-of-arrays, where the outer array contains 2 elements, both inner arrays are of equal length and the first inner array only contains integers and strings.
  • $ right_inverse将在数组数组上工作,其中外部数组包含2个元素,两个内部数组的长度相等,第一个内部数组只包含整数和字符串。

#5


2  

Strangely enough, the functions you're looking for are called array_keys() and array_values().

奇怪的是,您正在寻找的函数称为array_keys()和array_values()。

$keys = array_keys($array);
$vals = array_values($array);

#6


1  

function array_split($data)
{
  $x = 0;//Counter to ensure accuracy
  $retArray[0] = array();//Array of Keys
  $retArray[1] = array();//Array of Values

  foreach($data as $key => $value)
  {
    $retArray[0][$x] = $key;
    $retArray[1][$x] = $value;
    $x++;
  }

  RETURN $retArray;
}

$data = array("key" => "value", "key2" => "value2");

$splitData = array_split($data);

//print_r($splitData[0]);//Output: Array ( [0] => key [1] => key2 ) 
//print_r($splitData[1]);//Output: Array ( [0] => value [1] => value2 ) 

print_r($splitData);
//Output:
/*
Array
(
    [0] => Array
        (
            [0] => key
            [1] => key2
        )

    [1] => Array
        (
            [0] => value
            [1] => value2
        )

)
*/

#1


28  

There are two functions called array_keys and array_values:

有两个函数叫array_keys和array_values:

$array_keys = array_keys($array);
$array_values = array_values($array);

#2


7  

There are two functions actually:

实际上有两个功能:

$keys = array_keys($array);
$values = array_values($array);

You can also do the exact opposite:

你也可以这样做:

$array = array_combine($keys, $values);

#3


3  

use array_keys and array_values

使用array_keys和array_values

#4


2  

Unfortunately there is no built-in inverse of array_combine. There is also no way to define one, since array_combine expects multiple parameters and we can't return multiple values from a function.

不幸的是,没有array_combine的内置逆。也没有办法定义一个,因为array_combine需要多个参数,我们不能从函数返回多个值。

We can construct an alternative to array_combine which takes a single argument: the array of keys and the array of values wrapped up together in another array. This transformation is called "uncurrying" and is performed by the "call_user_func_array" function:

我们可以构造array_combine的替代方法,它接受一个参数:键数组和在另一个数组中包含在一起的值数组。此转换称为“uncurrying”,由“call_user_func_array”函数执行:

$array_comb  = function($arr) { return call_user_func_array('array_combine', $arr); };

This alternative function does have an inverse:

这个替代函数确实有一个逆:

$array_split = function($arr) { return array(array_keys($arr), array_values($arr)); };

If we define function composition:

如果我们定义函数组合:

$compose  = function($f, $g) {
    return function($x) use ($f, $g) { return $f($g($x)); };
};

Then the following functions are all (extensionally) equal, ie. they all return their argument unchanged:

然后,以下函数全部(扩展)相等,即。他们都不改变他们的论点:

$identity      = function($x) { return $x; };
$left_inverse  = $compose($array_split, $array_comb);  // Split then combine
$right_inverse = $compose($array_comb, $array_split);  // Combine then split

Note that they accept different argument types though:

请注意,它们接受不同的参数类型:

  • $identity will work on anything.
  • $ identity将适用于任何事情。
  • $left_inverse will work on any array.
  • $ left_inverse可以在任何数组上运行。
  • $right_inverse will work on arrays-of-arrays, where the outer array contains 2 elements, both inner arrays are of equal length and the first inner array only contains integers and strings.
  • $ right_inverse将在数组数组上工作,其中外部数组包含2个元素,两个内部数组的长度相等,第一个内部数组只包含整数和字符串。

#5


2  

Strangely enough, the functions you're looking for are called array_keys() and array_values().

奇怪的是,您正在寻找的函数称为array_keys()和array_values()。

$keys = array_keys($array);
$vals = array_values($array);

#6


1  

function array_split($data)
{
  $x = 0;//Counter to ensure accuracy
  $retArray[0] = array();//Array of Keys
  $retArray[1] = array();//Array of Values

  foreach($data as $key => $value)
  {
    $retArray[0][$x] = $key;
    $retArray[1][$x] = $value;
    $x++;
  }

  RETURN $retArray;
}

$data = array("key" => "value", "key2" => "value2");

$splitData = array_split($data);

//print_r($splitData[0]);//Output: Array ( [0] => key [1] => key2 ) 
//print_r($splitData[1]);//Output: Array ( [0] => value [1] => value2 ) 

print_r($splitData);
//Output:
/*
Array
(
    [0] => Array
        (
            [0] => key
            [1] => key2
        )

    [1] => Array
        (
            [0] => value
            [1] => value2
        )

)
*/