2015-10-09 60 views
0

我有一個這樣的陣列輸出;如何通過數組鍵值對數組元素進行分組?

[0] => Array 
    (
     [fis_id] => 16149 
     [fis_urun] => 129 
     [fis_tarihi] => 2015-10-05 17:57:03 
     [kargo_tarih] => 2015-10-09 10:07:43 
    ) 

[1] => Array 
    (
     [fis_id] => 16428 
     [fis_urun] => 128 
     [fis_tarihi] => 2015-10-07 18:45:31 
     [kargo_tarih] => 2015-10-09 09:21:18 
    ) 

[2] => Array 
    (
     [fis_id] => 16544 
     [fis_urun] => 90 
     [fis_tarihi] => 2015-10-08 18:23:05 
     [kargo_tarih] => 2015-10-09 10:16:15 
    ) 

[3] => Array 
    (
     [fis_id] => 16535 
     [fis_urun] => 91 
     [fis_tarihi] => 2015-10-08 17:44:34 
     [kargo_tarih] => 2015-10-09 09:16:39 
    ) 

[4] => Array 
    (
     [fis_id] => 16527 
     [fis_urun] => 129 
     [fis_tarihi] => 2015-10-08 17:09:20 
     [kargo_tarih] => 2015-10-09 09:22:40 
    ) 

[5] => Array 
    (
     [fis_id] => 16529 
     [fis_urun] => 90 
     [fis_tarihi] => 2015-10-08 17:11:49 
     [kargo_tarih] => 2015-10-09 09:19:00 
    ) 

在這個數組中,我想分組數組結果像;

product=>90 count(2) 

現在,我想fis_urun數組鍵重新安排我的陣列,組?那可能嗎 ?如果是我該怎麼辦?

謝謝。

+1

您可能必須做手工......循環在你的陣列,並填寫一個新的... – Random

+0

嗯,我真的不能? :( –

+0

增加了一組和排序循環 – Random

回答

1

有在PHP GROUPBY陣列中的最佳解決方案[添加類最近]

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
    </head> 
    <body> 
     <?php 
     // This example results in 
     // 1 Html table with some pretty hierarchy 
     // 2 "pretty" array 
     // 3 plain print_r of result 
     include "GroupBy.class.php"; 

     $dataRows = [ 
       [1, "Andy", "PHP", "4"], 
       [1, "Andy", "C#", "6"], 
       [2, "Josh", "C#", "6"], 
       [2, "Josh", "ASP", "4"], 
       [1, "Andy", "SQL", "6"], 
       [3, "Steve", "SQL", "2"], 
       [4, "self", "SQL", "30"], 
       [4, "self", "Smalltalk", "5"], 
       [4, "self", "C", "33"], 
       [4, "self", "Swedish", "65"] 
     ]; 
     // Group on the third column 
     $res = (new GroupBy())->groupBy($dataRows, [ 2 ]); 

     // Group on Years and Language 
// $res = (new GroupBy())->groupBy($dataRows, [ 3, 2 ]); 
     // You get an array back 
     // Each row contains two arrays 
     // First array: The key as an array of columns values 
     // Second array: An array of all rows to this key 


     // The above call [ 2 ] will result in 
     // (pasted from output of this test) 
     $x = [ 
       [ 
         ["ASP"], 
         [ 
           ["2", "Josh", "4"] 
         ] 
       ], 
       [ 
         ["C"], 
         [ 
           ["4", "self", "33"] 
         ] 
       ], 
       [ 
         ["C#"], 
         [ 
           ["1", "Andy", "6"], 
           ["2", "Josh", "6"] 
         ] 
       ], 
       [ 
         ["PHP"], 
         [ 
           ["1", "Andy", "4"] 
         ] 
       ], 
       [ 
         ["SQL"], 
         [ 
           ["1", "Andy", "6"], 
           ["3", "Steve", "2"], 
           ["4", "self", "30"] 
         ] 
       ], 
       [ 
         ["Smalltalk"], 
         [ 
           ["4", "self", "5"] 
         ] 
       ], 
       [ 
         ["Swedish"], 
         [ 
           ["4", "self", "65"] 
         ] 
       ] 
     ]; 
     // Usage (dummy) 
     foreach ($res as $aGroup) 
     { 
      $groupKey = $aGroup[0]; 
      $groupRows = $aGroup[1]; 
      foreach ($groupRows as $groupRow) 
      { 
       // We got all columns in $groupRow 
       // (And the key cols in $groupKey 
      } 
     } 
     // Display as a HTML table 

     echo "<code><table border='1'>"; 
     $runningLinenumber = 1; 
     foreach ($res as $aGroup) 
     { 
      $groupKey = $aGroup[0]; 
      $groupRows = $aGroup[1]; 
      // first row. calc rowspan 
      echo "<tr>"; 
      echo "<td>" . $runningLinenumber++ . "</td>"; // optional. but nice for user row interaction 
      echo "<td rowspan='" . count($groupRows) . "' valign=\"top\">" . implode(",", $groupKey) . "</td>"; 
      echo "<td>" . implode("</td><td>", $groupRows[0]) . "</td>"; 
      echo "</tr>"; 
      // end first row 
      for ($r = 1; $r < count($groupRows); ++$r) 
      { 
       $groupRow = $groupRows[$r]; 
       echo "<tr>"; 
       echo "<td>" . $runningLinenumber++ . "</td>"; // optional 
       echo "<td>" . implode("</td><td>", $groupRow) . "</td>"; 
       echo "</tr>"; 
      } 
      echo "</tr>"; 
     } 
     echo "</table></code>"; 

// Display as php array initial (copy and paste from the output) 

     echo '<pre>'; 
     echo "[\n"; 
     $keyAndData = []; 
     for ($grpNr = 0; $grpNr < count($res); ++$grpNr) 
     { 
      $groupKey = $res[$grpNr][0]; 
      $keyTTY = '"' . implode('","', $groupKey) . '"'; 
      echo " [\n"; 
      echo " [" . $keyTTY . "],\n"; 
      echo " [\n"; 
      $groupRows = $res[$grpNr][1]; 
      for ($rowNr = 0; $rowNr < count($groupRows); ++$rowNr) 
      { 
       $groupRow = $groupRows[$rowNr]; 
       $aRow = '"' . implode('","', $groupRow) . '"'; 
       echo " [" . $aRow . "]"; 
       if ($rowNr != count($groupRows) - 1) 
        echo ",\n"; 
       else 
        echo "\n"; 
      } 
      echo " ]\n"; 
      echo " ]"; 
      if ($grpNr != count($res) - 1) 
       echo ",\n"; 
      else 
       echo "\n"; 
     } 
     echo "]\n"; 
     echo print_r($res, true); 
     echo '</pre>'; 
     ?> 
</body> 
</html> 
+0

Dwnload鏈接,集團通過Array類: - http://www.phpclasses.org/package/9362-PHP-Group-bidimensional-arrays-by-a-given-element.html#view_files/文件/ 65914 –

+0

感謝。效果很好。 –

1

根據doc

$ar1 = array(10, 100, 100, 0); 
$ar2 = array(1, 3, 2, 4); 
array_multisort($ar1, $ar2); 

var_dump($ar1); 
var_dump($ar2); 

輸出:

array(4) { 
    [0]=> int(0) 
    [1]=> int(10) 
    [2]=> int(100) 
    [3]=> int(100) 
} 
array(4) { 
    [0]=> int(4) 
    [1]=> int(1) 
    [2]=> int(2) 
    [3]=> int(3) 
} 

所以,你所要做的就是讓包含你的鑰匙一個新的數組,並按照在第一陣列根據新一個訂單。
但首先,你必須重建你的當前數組。

foreach($myMultiArray as $index => $values) { 
    $myGroupedArray[$values['fis_urun']][] = $values 
} 

這會讓你的陣列是這樣的:

[129] => Array 
    (
    [0] => Array 
     (
      [fis_id] => 16149 
      [fis_urun] => 129 
      [fis_tarihi] => 2015-10-05 17:57:03 
      [kargo_tarih] => 2015-10-09 10:07:43 
     ) 
    [1] => Array 
     (
      [fis_id] => 16527 
      [fis_urun] => 129 
      [fis_tarihi] => 2015-10-08 17:09:20 
      [kargo_tarih] => 2015-10-09 09:22:40 
     ) 
    ) 

[128] => Array 
    (
    [0] => Array 
     (
      [fis_id] => 16428 
      [fis_urun] => 128 
      [fis_tarihi] => 2015-10-07 18:45:31 
      [kargo_tarih] => 2015-10-09 09:21:18 
     ) 
    ) 

[90] => Array 
    (
    [0] => Array 
     (
     [fis_id] => 16544 
     [fis_urun] => 90 
     [fis_tarihi] => 2015-10-08 18:23:05 
     [kargo_tarih] => 2015-10-09 10:16:15 
     ) 
    [1] => Array 
     (
      [fis_id] => 16529 
      [fis_urun] => 90 
      [fis_tarihi] => 2015-10-08 17:11:49 
      [kargo_tarih] => 2015-10-09 09:19:00 
     ) 

[91] => Array 
    (
    [0] => Array 
     (
      [fis_id] => 16535 
      [fis_urun] => 91 
      [fis_tarihi] => 2015-10-08 17:44:34 
      [kargo_tarih] => 2015-10-09 09:16:39 
     ) 
    ) 

那麼你就必須對它進行排序:(使用數組multisort或ksort,取決於你希望你的最終陣列1級的密鑰即(0,1,2,3)或(90,91,128,129)

foreach($myGroupedArray as $grouped_fis_urun => $values) { 
    $myFis_urun[] = $grouped_fis_urun; 
} 
array_multisort($myGroupedArray, $myFis_urun); 

它應該做的伎倆...

+0

感謝真正明確的答案 –

1

這可能工作:

foreach ($your_array as $key => $val) { 
    $newArr[$value['fis_urun']] = $value 
} 
+0

我認爲這組值相同的密鑰和'$ newArr'這是需求推動它。只是給了一個嘗試。希望這有助於! – jitendrapurohit

+0

。幹得好感謝 –

相關問題