PHP Array Distinct元素分解

时间:2017-07-10 21:53:23

标签: php arrays

我动态地从数据库中提取结果并将它们放在数组$ myArray中,如下所示

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

理想的做法是创建一个新数组,使原始数据不受影响

$ 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-->

1 个答案:

答案 0 :(得分:1)

首先结合相同水果的阵列。现在,您将在单个数组下拥有相同的多个水果数组。然后只需迭代该数组&amp;格式化响应。

$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);