2013-07-20 73 views
0

我想按關鍵性別和鑰匙RNAME從以下關聯數組數二的鍵 - >值過濾器,請參考下面...陣列計數誤差

<pre> 
Array 
(
    [0] => Array 
     (
      [fname] => jone 
      [lname] => jani 
      [gender] => m 
      [rname] => N 
     ) 

    [1] => Array 
     (
      [fname] => jani 
      [lname] => jone 
      [gender] => m 
      [rname] => N 
     ) 

    [2] => Array 
     (
      [fname] => sara 
      [lname] => bulbula 
      [gender] => w 
      [rname] => D 
     ) 

    [3] => Array 
     (
      [fname] => hani 
      [lname] => hanu 
      [gender] => w 
      [rname] => P 
     ) 

    [4] => Array 
     (
      [fname] => ttttttt 
      [lname] => sssssss 
      [gender] => m 
      [rname] => C 
     ) 

) 
</pre> 

我想要的是什麼結果如下..

 
rname gender 
     m w 
N  2 0 
D  0 1 
P  0 1 
C  1 0 
total 3 2 

有些幫助嗎?

+2

所以哪裏是你的代碼有錯誤? – bitWorking

+0

我可以指望只在一個鍵值...但我有兩種。我使用這個小功能coudn't,但只在一個Key..public功能array_column($數組$鍵){$ 場計數=陣列(); 的foreach($數組作爲$ realKey => $值){ 如果(isset($值[$鍵])){$ 領域[$ realKey] = $值[$鍵]。 } } return $ field; } –

回答

1

我沒有看到你的代碼,所以我試圖讓自己的代碼。

$arr = array(
    array(
     'fname' => 'jone', 
     'lname' => 'jani', 
     'gender' => 'm', 
     'rname' => 'N' 
    ), 
    array(
     'fname' => 'jani', 
     'lname' => 'jone', 
     'gender' => 'm', 
     'rname' => 'N' 
    ), 
    array(
     'fname' => 'sara', 
     'lname' => 'bulbula', 
     'gender' => 'w', 
     'rname' => 'D' 
    ), 
    array(
     'fname' => 'hani', 
     'lname' => 'hanu', 
     'gender' => 'w', 
     'rname' => 'P' 
    ), 
    array(
     'fname' => 'ttttttt', 
     'lname' => 'sssssss', 
     'gender' => 'm', 
     'rname' => 'C' 
    ), 
); 

$matrix = array(); 
foreach (array('N', 'D', 'P', 'C', 'total') as $name) { 
    foreach (array('m', 'w') as $gender) { 
     $matrix[$name][$gender] = 0; 
    } 
} 

foreach ($arr as $el) { 
    $matrix[$el['rname']][$el['gender']] += 1; 
    $matrix['total'][$el['gender']] += 1; 
} 


echo '<pre>'; 
print_r($matrix); 
echo '</pre>'; 

// OR dynamicaly keys 

$matrix = array(); 
foreach ($arr as $el) { 
    if (!isset($matrix[$el['rname']][$el['gender']])) 
     $matrix[$el['rname']][$el['gender']] = 0; 

    if (!isset($matrix['total'][$el['gender']])) 
     $matrix['total'][$el['gender']] = 0; 

    $matrix[$el['rname']][$el['gender']] += 1; 
    $matrix['total'][$el['gender']] += 1; 
} 


echo '<pre>'; 
print_r($matrix); 
echo '</pre>'; 
+0

你的第一個的foreach不會動態創建基於rnames計數,如果有引入'x'例如一個RNAME會發生什麼?你將需要修改第一個foreach接受這個參數 –

+0

這真的是很好的解決方案男人,謝謝。 Mr.Slawomir。 –

1

我有我已經習慣了做類似你的要求的東西有類似的功能。所以這只是一個複製和粘貼,因爲你表現出缺乏代碼..這將返回數組作爲你的期待..只是遍歷這個返回的數組,有幾分像,隨後

echo "<pre>"; 
    print_r($Array_Count); 
echo "</pre>"; 

返回,您將需要研究到自己

function Array_Counter($Array){ 
     $Return_Arr = array(
      "M" => array(), 
      "F" => array() 
     ); 
    foreach ($Array AS $Arr){ 
     if ($Arr['gender'] === "m"){ 
      //Is Male 
      @$Return_Arr['M'][$Arr['rname']] ++; // Message supressed due to returning undefined index warnings... Which because of not using isset().. Will still work 
     }elseif ($Arr['gender'] === "f"){ 
      // Is Female 
      @$Return_Arr['F'][$Arr['rname']] ++; // Message supressed due to returning undefined index warnings... Which because of not using isset().. Will still work 
     } 

    } 
    return $Return_Arr; 

} 

$Array = array (
    array(
     "gender" => "m", 
     "rname" => "c" 
    ), 
    array(
     "gender"=> "f", 
     "rname" => "d" 
    ), 
    array (
     "gender" => "f", 
     "rname" => "a" 
    ), 
     array (
     "gender" => "f", 
     "rname" => "x" 
    ), 
     /* Array shortened for exampling purposes */ 

); 


$Array_Count = Array_Counter($Array); 

使用功能:

Array 
(
    [M] => Array 
     (
      [c] => 1 
      [j] => 1 
      [g] => 1 
      [d] => 1 
     ) 

    [F] => Array 
     (
      [d] => 3 
      [a] => 1 
      [x] => 1 
     ) 

)