0

我正在尝试通过 Key-gender 和 Key-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>  

我想要的结果如下..

重新命名性别
        兆瓦
氮2 0
0 1
0 1
1 0
总计 3 2

请帮忙?

4

2 回答 2

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
        )

)
于 2013-07-20T23:14:12.183 回答
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>';
于 2013-07-20T23:16:14.297 回答