I have an array that has keys and values. For eg:
我有一个具有键和值的数组。例如:
Array (
[name] => aalaap
[age] => 29
[location] => mumbai
)
I want to convert the keys from this into values, but I want the values to apear right after the keys. For eg:
我想将密钥从此转换为值,但我希望值在密钥之后立即显示。例如:
Array (
[0] => name
[1] => aalaap
[2] => age
[3] => 29
[4] => location
[5] => mumbai
)
I can easily write an iteration function that will do this... for eg:
我可以轻松编写一个迭代函数来执行此操作...例如:
array_flatten($arr) {
foreach ($arr as $arrkey => $arrval) {
$arr_new[] = $arrkey;
$arr_new[] = $arrval;
}
return $arr_new;
}
...but I'm trying to find out if there's any way this can be accomplished using array_combine
, array_keys
, array_values
and/or array_merge
, preferably in one, so i don't need to use a custom function.
...但我试图找出是否有任何方法可以使用array_combine,array_keys,array_values和/或array_merge完成,最好是在一个,所以我不需要使用自定义函数。
Is there?
5 个解决方案
#1
Your own solution is probably the cleanest solution, so converting it to a "one-liner":
您自己的解决方案可能是最干净的解决方案,因此将其转换为“单线程”:
$array = array('name' => 'aalaap','age' => 29, 'location' => 'mumbai');
$answer = array();
array_walk($array, create_function('$val,$key', 'global $answer; $answer[]=$key; $answer[]=$val;'));
var_dump($answer);
This avoids unnecessary and expensive array copies or sorting.
这避免了不必要且昂贵的阵列复制或排序。
Alternatively, lose the global:
或者,失去全球:
array_walk($array, create_function('$val,$key,$result', '$result[]=$key; $result[]=$val;'), &$answer);
#2
PHP 5.3+ version of Just Jules' answer, and a bit more readable:
Just Jules答案的PHP 5.3+版本,更具可读性:
array_walk($array, function($val, $key) use (&$answer) {
$answer[] = $key;
$answer[] = $val;
});
#3
I don't think this is possible - with the built-in functions you'll end up with all the keys then all the values:
我不认为这是可能的 - 使用内置函数,您将获得所有键,然后是所有值:
$a = array('a' => 'A', 'b' => 'B', 'c' => 'C');
$a = array_merge(array_keys($a), array_values($a));
print_r($a);
You're going to have to use a loop like this:
你将不得不使用这样的循环:
$b = array();
foreach ($a as $key => $value)
{
$b[] = $key;
$b[] = $value;
}
#4
It is possible, but I don't think it is more readable or any faster. It would work with a less-known feature of PHP - the array addition:
这是可能的,但我不认为它更具可读性或更快。它可以使用PHP的一个鲜为人知的功能 - 数组添加:
$array = array('name' => 'aalaap', 'age' => 29, 'location' => 'mumbai');
# Separate keys and values into distinct arrays
$keys = array_keys($array);
$values = array_values($array);
# Generate 2 new array containing indexes for the 2 arrays which contain
# only odd/even numbers:
$keysKeys = range(0, count($keys) * 2 - 1, 2);
$valuesKeys = range(1, count($keys) * 2, 2);
# Combine the keys with the values and add the results:
$array = array_combine($keysKeys, $keys) + array_combine($valuesKeys, $values);
# Sort the resulting array, otherwise the numbering will be broken
# (1,3,5,2,4,6)
ksort($array);
# Result:
var_dump($array);
array(6) {
[0]=>
string(4) "name"
[1]=>
string(6) "aalaap"
[2]=>
string(3) "age"
[3]=>
int(29)
[4]=>
string(8) "location"
[5]=>
string(6) "mumbai"
}
#5
Could use an array_reduce to get close.
可以使用array_reduce来关闭。
array_reduce(array_keys($arr), function($carry, $key)use($arr){
$carry[] = $key;
$carry[] = $arr[$key];
return $carry;
}, array());
#1
Your own solution is probably the cleanest solution, so converting it to a "one-liner":
您自己的解决方案可能是最干净的解决方案,因此将其转换为“单线程”:
$array = array('name' => 'aalaap','age' => 29, 'location' => 'mumbai');
$answer = array();
array_walk($array, create_function('$val,$key', 'global $answer; $answer[]=$key; $answer[]=$val;'));
var_dump($answer);
This avoids unnecessary and expensive array copies or sorting.
这避免了不必要且昂贵的阵列复制或排序。
Alternatively, lose the global:
或者,失去全球:
array_walk($array, create_function('$val,$key,$result', '$result[]=$key; $result[]=$val;'), &$answer);
#2
PHP 5.3+ version of Just Jules' answer, and a bit more readable:
Just Jules答案的PHP 5.3+版本,更具可读性:
array_walk($array, function($val, $key) use (&$answer) {
$answer[] = $key;
$answer[] = $val;
});
#3
I don't think this is possible - with the built-in functions you'll end up with all the keys then all the values:
我不认为这是可能的 - 使用内置函数,您将获得所有键,然后是所有值:
$a = array('a' => 'A', 'b' => 'B', 'c' => 'C');
$a = array_merge(array_keys($a), array_values($a));
print_r($a);
You're going to have to use a loop like this:
你将不得不使用这样的循环:
$b = array();
foreach ($a as $key => $value)
{
$b[] = $key;
$b[] = $value;
}
#4
It is possible, but I don't think it is more readable or any faster. It would work with a less-known feature of PHP - the array addition:
这是可能的,但我不认为它更具可读性或更快。它可以使用PHP的一个鲜为人知的功能 - 数组添加:
$array = array('name' => 'aalaap', 'age' => 29, 'location' => 'mumbai');
# Separate keys and values into distinct arrays
$keys = array_keys($array);
$values = array_values($array);
# Generate 2 new array containing indexes for the 2 arrays which contain
# only odd/even numbers:
$keysKeys = range(0, count($keys) * 2 - 1, 2);
$valuesKeys = range(1, count($keys) * 2, 2);
# Combine the keys with the values and add the results:
$array = array_combine($keysKeys, $keys) + array_combine($valuesKeys, $values);
# Sort the resulting array, otherwise the numbering will be broken
# (1,3,5,2,4,6)
ksort($array);
# Result:
var_dump($array);
array(6) {
[0]=>
string(4) "name"
[1]=>
string(6) "aalaap"
[2]=>
string(3) "age"
[3]=>
int(29)
[4]=>
string(8) "location"
[5]=>
string(6) "mumbai"
}
#5
Could use an array_reduce to get close.
可以使用array_reduce来关闭。
array_reduce(array_keys($arr), function($carry, $key)use($arr){
$carry[] = $key;
$carry[] = $arr[$key];
return $carry;
}, array());