0

I am trying to count from the following associative array in two's key->value filter, by Key-gender and Key-rname, Please see below ...

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

What i want the result is as follows..

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

Some help please?

2
  • 2
    so where's your code that has an error? Commented Jul 20, 2013 at 22:55
  • I can count only in one key-value...but i coudn't in two .i use this small function, but counts only in one Key..public function array_column($array, $key) { $field = array(); foreach($array as $realKey => $value) { if (isset($value[$key])) { $field[$realKey] = $value[$key]; } } return $field; } Commented Jul 20, 2013 at 23:13

2 Answers 2

1

I don't see your code so I am trying to make own code.

$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>';
Sign up to request clarification or add additional context in comments.

2 Comments

Your first foreach does not dynamically create a count based on rnames, what happens if there is an rname introduced as x for example? You will need to modify the first foreach to accept this argument
That is really nice solution Men, Thanks. Mr.Slawomir.
1

I have a similar function which I have used to do something similar to your request.. So this is just a copy and paste, because you have shown a lack of code.. This returns an array as your expecting.. Just iterating over this returned array, is something you will need to research into yourself

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

Using the function as followed

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

Returns:

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

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

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.