如何将数组父亲的键与子元素的键交换
How to exchange a key from an array father with an key of child element
我想用父元素的键交换子元素的键,可以吗?
代码:
$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white"));
print_r($array);
这就是我想要做的:
感谢您的帮助
如果您的数组名为 $arrayOne
,那么您可以创建 $arrayTwo
(其中包含键的开关),如下所示:
<?php
$arrayTwo = [];//initiating the array which will hold the solution
foreach($arrayOne as $keyFather=>$valueFather)
{
$newKey = $valueFather[0];
$arrayTwo = ($newKey => array());//giving child key to the original
//position of the father key
//and creating a new array inside
$firstIteration = TRUE;//conditional which helps place the fathers
//key into the previous child key position
//filling up the new array created:
foreach($valueFather as $key=$otherValue)
{
$nextKey = $key;
if ($firstIteration)//only iterates at the beginning of the loop
{
$firstIteration = FALSE;
$nextKey = $keyFather;//giving father key to child key position
}
$arrayTwo[$newKey][$nextKey] = $otherValue;//saving results
}
}
?>
像这样:
<?php
$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white"));
$new_array = array();
foreach( $array as $key => $value){
foreach ( $value as $k => $v ){
$new_array[$k] = $value;
foreach ( $new_array[$k] as $k1 => $v1){
$new_array[$k][$key] = $v1;
unset($new_array[$k][$k1]);
break;
}
break;
}
}
print_r($new_array);
?>
打印:
Array
(
[a] => Array
(
[b] => green
[c] => blue
[d] => yellow
[0] => red
)
[e] => Array
(
[f] => cyan
[g] => brown
[h] => white
[1] => purple
)
)
如果您只想将父键与 "child" 数组中的第一个键交换 - 这是使用 array_map
、array_walk
、array_shift
、[=15 的解决方案=]、array_values
和 current
函数:
$keys = array_map("key", $array);
array_walk($array, function(&$v, $k){
$value = current($v);
array_shift($v);
$v[$k] = $value;
ksort($v, SORT_NATURAL);
});
$result = array_combine($keys, array_values($array));
print_r($result);
输出:
Array
(
[a] => Array
(
[0] => red
[b] => green
[c] => blue
[d] => yellow
)
[e] => Array
(
[1] => purple
[f] => cyan
[g] => brown
[h] => white
)
)
解决方法是:
- 首先获取子数组的第一个key
- 向数组追加一个新的子数组。
- 向子数组追加一个新元素。
- 从子数组中删除旧元素。
- 最后,删除旧的子数组。
所以你的代码应该是这样的:
$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white"));
foreach($array as $key => $arr){
$k = key($arr); // get the first key of the subarray
$array[$k] = $arr; // append a new subarray to the array
$array[$k][$key] = $arr[$k]; // apppend a new element to the subarray
unset($array[$k][$k]); // delete the old element from the subarray
unset($array[$key]); // delete the old subarray
}
// display $array
echo '<pre>';
print_r($array);
echo '</pre>';
我想用父元素的键交换子元素的键,可以吗?
代码:
$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white"));
print_r($array);
这就是我想要做的:
感谢您的帮助
如果您的数组名为 $arrayOne
,那么您可以创建 $arrayTwo
(其中包含键的开关),如下所示:
<?php
$arrayTwo = [];//initiating the array which will hold the solution
foreach($arrayOne as $keyFather=>$valueFather)
{
$newKey = $valueFather[0];
$arrayTwo = ($newKey => array());//giving child key to the original
//position of the father key
//and creating a new array inside
$firstIteration = TRUE;//conditional which helps place the fathers
//key into the previous child key position
//filling up the new array created:
foreach($valueFather as $key=$otherValue)
{
$nextKey = $key;
if ($firstIteration)//only iterates at the beginning of the loop
{
$firstIteration = FALSE;
$nextKey = $keyFather;//giving father key to child key position
}
$arrayTwo[$newKey][$nextKey] = $otherValue;//saving results
}
}
?>
像这样:
<?php
$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white"));
$new_array = array();
foreach( $array as $key => $value){
foreach ( $value as $k => $v ){
$new_array[$k] = $value;
foreach ( $new_array[$k] as $k1 => $v1){
$new_array[$k][$key] = $v1;
unset($new_array[$k][$k1]);
break;
}
break;
}
}
print_r($new_array);
?>
打印:
Array
(
[a] => Array
(
[b] => green
[c] => blue
[d] => yellow
[0] => red
)
[e] => Array
(
[f] => cyan
[g] => brown
[h] => white
[1] => purple
)
)
如果您只想将父键与 "child" 数组中的第一个键交换 - 这是使用 array_map
、array_walk
、array_shift
、[=15 的解决方案=]、array_values
和 current
函数:
$keys = array_map("key", $array);
array_walk($array, function(&$v, $k){
$value = current($v);
array_shift($v);
$v[$k] = $value;
ksort($v, SORT_NATURAL);
});
$result = array_combine($keys, array_values($array));
print_r($result);
输出:
Array
(
[a] => Array
(
[0] => red
[b] => green
[c] => blue
[d] => yellow
)
[e] => Array
(
[1] => purple
[f] => cyan
[g] => brown
[h] => white
)
)
解决方法是:
- 首先获取子数组的第一个key
- 向数组追加一个新的子数组。
- 向子数组追加一个新元素。
- 从子数组中删除旧元素。
- 最后,删除旧的子数组。
所以你的代码应该是这样的:
$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white"));
foreach($array as $key => $arr){
$k = key($arr); // get the first key of the subarray
$array[$k] = $arr; // append a new subarray to the array
$array[$k][$key] = $arr[$k]; // apppend a new element to the subarray
unset($array[$k][$k]); // delete the old element from the subarray
unset($array[$key]); // delete the old subarray
}
// display $array
echo '<pre>';
print_r($array);
echo '</pre>';