2017-06-10 62 views
0

最近,我正在開發一個項目,根據他們的'Franchaise'和'Superpower'的來源對'Superheros'和'Supervillains'進行分類。我想從數據庫中獲取數據,如數組#1,並將它們顯示爲數組#2在PHP中。在php和count數據中對數組進行分組

Array #1 

Array 
(
    [0] => Array 
    (
     [id] => 101 
     [Name] => Superman 
     [Franchise] => DC Comics 
     [Superpower] => Inherent 
    ) 

    [1] => Array 
    (
     [id] => 908 
     [Name] => Batman 
     [Franchise] => DC Comics 
     [Superpower] => Acquired 
    ) 

    [2] => Array 
    (
     [id] => 228 
     [Name] => Wolverine 
     [Franchise] => Marvel 
     [Superpower] => Acquired 
    ) 

    [3] => Array 
    (
     [id] => 158 
     [Name] => Iron Man 
     [Franchise] => Marvel 
     [Superpower] => Acquired 
    ) 

    [4] => Array 
    (
     [id] => 978 
     [Name] => Thor 
     [Franchise] => Marvel 
     [Superpower] => Inherent 
    ) 
) 

陣列#1的元素必須分組基於他們的「特權」,並指望有多少人是「固有」或在「超級大國」術語「後天」。

Array #2 


Array 
(
    [DC Comics] => Array 
    (
     [Inherent] => 1 
     [Acquired] => 1 
    ) 

    [Marvel] => Array 
    (
     [Inherent] => 1 
     [Acquired] => 2 
    ) 
) 
+1

哪裏是你的代碼的企圖? – MikeBergerUS

+1

托爾沒有繼承他的超級大國。他被給了Mjolnir。 – Goose

+0

你想要算什麼? 數組或相同鍵的數組? –

回答

1

這裏是你的代碼沒有太多的邏輯,

foreach ($array1 as $val) { 
    if(!isset($array2[$val['Franchise']])) { 
     $array2[$val['Franchise']] = array('Inherent' => 0, 'Acquired' => 0); 
    } 
    $array2[$val['Franchise']][$val['Superpower']]++; 
} 

print_r($array2); 
+0

這正是我期待的,簡單而緊湊的。謝謝.. – Satya

1
  1. array_column()功能聚集來自2D陣列的內鍵。

  2. array_count_values()函數計算一個數組的所有值。

使用計數值此代碼段:

$a = array (array ("id" => "101", "Name" => "Superman", "Franchise" => "DC Comics", "Superpower" => "Inherent"), 
    array ("id" => "908", "Name" => "Batman", "Franchise" => "DC Comics", "Superpower" => "Acquired"), 
    array ("id" => "228", "Name" => "Wolverine", "Franchise" => "Marvel", "Superpower" => "Acquired"), 
    array ("id" => "158", "Name" => "Iron Man", "Franchise" => "Marvel", "Superpower" => "Acquired"), 
    array ("id" => "978", "Name" => "Thor", "Franchise" => "Marvel", "Superpower" => "Inherent")); 
    echo "<pre>"; 
$return = array(); 
// first group array values by franchise 
foreach($a as $val) { 
    if (!isset($return[$val['Franchise']])) { 
     $return[$val['Franchise']] = array(); 
    } 
    $return[$val['Franchise']][] = $val; 
} 
$arr = array(); 
// count elements by key value pair in particular franchise 
foreach ($return as $key => $value) { 
    $tmp = array_count_values(array_column($value, 'Superpower')); 
    $arr[$key] = $tmp; 
} 
print_r($arr); 

Demo is here

1

隨着單和短array_reduce

// $arr is your initial array 
$result = array_reduce($arr, function($r, $v){ 
    if (isset($r[$v["Franchise"]][$v["Superpower"]])) { 
     $r[$v["Franchise"]][$v["Superpower"]] += $r[$v["Franchise"]][$v["Superpower"]]; 
    } else { 
     $r[$v["Franchise"]][$v["Superpower"]] = 1; 
    } 
    return $r; 
}, []); 

print_r($result); 

輸出:

Array 
(
    [DC Comics] => Array 
     (
      [Inherent] => 1 
      [Acquired] => 1 
     ) 

    [Marvel] => Array 
     (
      [Acquired] => 2 
      [Inherent] => 1 
     ) 
) 
相關問題