子要素のキーで父からキーを交換したいと思いますか?配列からキーを子要素のキーで交換する方法
コード:
$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white"));
print_r($array);
これは私が何をしたいです:ヘルプ
子要素のキーで父からキーを交換したいと思いますか?配列からキーを子要素のキーで交換する方法
コード:
$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white"));
print_r($array);
これは私が何をしたいです:ヘルプ
:
<?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
)
)
ありがとうございました!あなたは私を助けました、それは私が望んだものに非常に似ています! –
ため
おかげで、あなたの配列は、あなたが作成することができ、$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
}
}
?>
あなただけの "子" の配列から最初のキーと親キーを入れ替えることを意味した場合 - ここソリューションはarray_map
を使用しています、 array_walk
,array_shift
,array_combine
,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
)
)
解決策は以下のとおりです。
だからあなたのコードは次のようにする必要があります:
$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>';
ここで `について...赤[B] => ...`、** * bのだろうかLive Demo
です*同じままにするか、それとも父親との最初の子ですか? – Webeng
今私の答えを編集しました。それが動作すれば教えてください。 – Webeng