2016-05-12 6 views
0

子要素のキーで父からキーを交換したいと思いますか?配列からキーを子要素のキーで交換する方法

コード:

$array = array(array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"), array("e"=>"purple","f"=>"cyan","g"=>"brown","h"=>"white")); 

print_r($array); 

これは私が何をしたいです:ヘルプ

+0

です*同じままにするか、それとも父親との最初の子ですか? – Webeng

+0

今私の答えを編集しました。それが動作すれば教えてください。 – Webeng

答えて

1

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

) 
+1

ありがとうございました!あなたは私を助けました、それは私が望んだものに非常に似ています! –

1

ため

enter image description here

おかげで、あなたの配列は、あなたが作成することができ、$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 
    } 
    } 

?> 
1

あなただけの "子" の配列から最初のキーと親キーを入れ替えることを意味した場合 - ここソリューションは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 
     ) 
) 
1

解決策は以下のとおりです。

  • まず部分配列の最初のキーを取得します。
  • 新しいサブアレイを配列に追加します。
  • サブアレイに新しい要素を追加します。
  • サブアレイから古い要素を削除します。
  • 最後に、古いサブアレイを削除します。

だからあなたのコードは次のようにする必要があります:

$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

関連する問題