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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6234696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:43:29  来源:igfitidea点击:

PHP Splitting an Array into two arrays - keys array and values array

phparrays

提问by Bululu

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

回答by Yuri Stuken

There are two functions called array_keysand array_values:

有两个函数称为 array_keysarray_values

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

回答by netcoder

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

回答by Samuel Herzog

回答by Sam Dufel

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

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

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

回答by Warbo

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.
  • $left_inverse will work on any array.
  • $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.
  • $identity 可以处理任何事情。
  • $left_inverse 将适用于任何数组。
  • $right_inverse 将适用于数组数组,其中外部数组包含 2 个元素,两个内部数组的长度相等,第一个内部数组仅包含整数和字符串。

回答by user3289003

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
        )

)
*/