0

I have this following Array. Here, i want to count the variable fraction which has the value 1. Based on the no.of.occurances, i want to do some operations. The one way i know is, to loop the array by using foreach and put an incrementor to identify how many fraction has the value 1. But i want to know is there any easier way than the above one?

could anyone suggest me?

Array
(
    [0] => Array
        (
            [answer] => <p>
    Quiz Test Interoperability</p>

            [feedback] => 
            [fraction] => 0
        )

    [1] => Array
        (
            [answer] => <p>
    Quiz Test Interrelationship</p>

            [feedback] => <br />

            [fraction] => 0
        )

    [2] => Array
        (
            [answer] => <p>
    Quiz Team Interoperability</p>

            [feedback] => <br />

            [fraction] => 0
        )

    [3] => Array
        (
            [answer] => <p>
    Queation Test Interoperability</p>

            [feedback] => <br />

            [fraction] => 1
        )

)

1 Answer 1

4

You can use array_reduce():

<?php
function countFractionIs1 ($element) {
   return ($element['fraction'] == 1) ? 1 : 0;
}

$count = array_reduce($yourarray, "countFractionIs1", 0);
Sign up to request clarification or add additional context in comments.

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.