2017-07-10 24 views
-1
私は動的にデータベースから結果を引き出し、

PHP配列個別要素崩壊

Array ( 
    [0] => Array ( 
     [0] => Array ( 
       [Fruit] => Apple 
       [Number] => 1 
       [Date] => 3117-01-41 
       [supplier] => Store 1 
       [description] => SAmple text for apple ) 
     [1] => Array ( 
       [Fruit] => Orange 
       [Number] => 1932 
       [Date] => 3117-01-41 
       [supplier] => Store 2 
       [description] => Sample text for Orange ) 
     [2] => Array ( 
       [Fruit] => Grape 
       [Number] => 22 
       [Date] => 3117-01-41 
       [supplier] => Store Street 
       [description] => Sample Text for Grape ) 
     [3] => Array ( 
       [Fruit] => Apple 
       [Number] => 23 
       [Date] => 3117-01-41 
       [supplier] => Store 9 
       [description] => This is text for a second apple) 
     [4] => Array ( 
       [Fruit] => Apple 
       [Number] => 49 
       [Date] => 3117-01-41 
       [supplier] => Store 007 
       [description] => This is more text for some apples ) 
     [5] => Array ( 
       [Fruit] => Orange 
       [Number] => 1 
       [Date] => 3117-01-41 
       [supplier] => Store 7 
       [description] => This is for orange also ) 
     ) 
) 

下に理想的で新しい配列を作成することですように見える配列$ myarrayの、でそれらを置く

そのまま元のままので、

$ newArray = $ myArray;

私がしたいことは新しい配列にあります。例えば、 'Fruit'のような別のフィールドを選択すると、別のフィールドが表示されますが、別のフィールドが異なる場合は、新しい配列内に入れ子になった配列を作成します。

<!-- Desired output of $newArray --> 
[0] => Array ( 
      [Fruit] => Apple 
      [Number] => Array 
       (
       [0] => 1 
       [1] => 23 
       [2] => 49 
      ) 
      [Date] => Array 
       (
       [0] => 3117-01-41 
       [1] => 3117-01-41 
       [2] => 3117-01-41 
      ) 
      [supplier] => Array 
       (
       [0] => Store 1 
       [1] => Store 9 
       [2] => Store 007 
      ) 
      [description] => Array 
       (
       [0] => SAmple text for apple 
       [1] => This is text for a second apple 
       [2] => This is more text for some apples 
      ) ) 
[1] => Array ( 
       [Fruit] => Orange 
       [Number] => Array 
       (
       [0] => 1932 
       [1] => 1 

      ) 
       [Date] => Array 
       (
       [0] => 3117-01-41 
       [1] => 3117-01-41 
       [2] => 3117-01-41 
      ) 
       [supplier] => Array 
      (
      [0] => Store 2 
      [1] => Store 7 

     ) 
       [description] => Array 
       (
       [0] => Sample text for Orange 
       [1] => This is for orange also 

      ) ) 
<!--Grape contents--> 
+0

このタスクを解決しようとしたときに書いたコードはどこですか? – arkascha

+0

どこで起動するのかわからない – jumpman8947

+0

まあ、すべての配列要素に対してループを繰り返し作成し、最後の配列に追加する各繰り返しで新しい要素を作成します。そして、あなたは各属性を一つずつ保存します。 – arkascha

答えて

1

最初に同じ果物の配列を組み合わせます。今度は、単一のアレイの下で同じ複数の果物アレイを持つでしょう。次に、配列&を応答してレスポンスをフォーマットします。

$tempArray = []; 
//creates the array of common fruit 
foreach ($myArray[0] as $key => $value) { 
    $tempArray[$value['Fruit']][] = $value; 
} 

$newarray = []; 
//iterate the new array of array of fruits 
foreach ($tempArray as $key => $value) { 
    //here value will be common fruit array so iterate & format the array 
    $temp = []; 
    $temp['Fruit'] = $value[0]['Fruit']; 
    foreach ($value as $key => $val) { 
     $temp['Number'][] = $val['Number']; 
     $temp['Date'][] = $val['Date']; 
     $temp['supplier'][] = $val['supplier']; 
     $temp['description'][] = $val['description']; 
    } 
    $newarray [] = $temp; 
} 
var_dump($newarray);