数组计数错误

时间:2013-07-20 22:49:27

标签: php arrays associative-array

我试图通过Key-gender和Key-rname从两个key->值过滤器中的以下关联数组中计算,请参阅下面的内容......

<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 个答案:

答案 0 :(得分:1)

我有一个类似的功能,我曾经做过类似你的请求。所以这只是一个复制和粘贴,因为你已经显示缺乏代码..这返回一个数组作为你的期望..只是迭代这个返回的数组,你需要自己研究

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

使用以下功能

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

返回:

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

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

)

答案 1 :(得分: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>';