1

I have multiple values in a list now I want to count how many times 100 occurs in this list (using PHP).

My PHP Code for this:

<table class="widefat" style="margin-top:50px;">

Output:

1      33,55,88,100,66,4
       45,45,58,49,49,4
       100,100,50,49,80,4


2      33,55,88,100,66,4
       45,45,58,49,49,4
       100,100,50,49,80,4


3      33,55,88,100,66,4
       45,45,58,49,49,4
       100,100,50,49,80,4

I want Output to be:

1      100 (1) Times
       100 (0) Times
       100 (2) Times


2      100 (1) Times
       100 (0) Times
       100 (2) Times

3      100 (1) Times
       100 (0) Times
       100 (2) Times
1
  • I have a variable $a when i echo this they print this value. echo $a; Commented Mar 30, 2019 at 21:48

2 Answers 2

0

You could make use of a combination of explode, array_map, array_filter and array_count_values to split the values on a comma and keep the ones for which the values are 100.

For example:

$items = [
    1 => [
        "33,55,88,100,66,4",
        "45,45,58,49,49,4",
        "100,100,50,49,80,4"
    ],
    2 => [
        "33,55,88,100,66,4",
        "45,45,58,49,49,4",
        "100,100,50,49,80,4"
    ],
    3 => [
        "33,55,88,100,66,4",
        "45,45,58,49,49,4",
        "100,100,50,49,80,4"
    ],
];

foreach ($items as &$item) {
    $item = array_map(function ($x) {
        $res = array_filter(array_count_values(
            explode(',', $x)
        ), function ($key) {
            return $key === 100;
        }, ARRAY_FILTER_USE_KEY);
        return sprintf("100 (%s) Times", count($res) === 1 ? $res[100] : 0);
    }, $item);
}

print_r($items);

Result

Array
(
    [1] => Array
        (
            [0] => 100 (1) Times
            [1] => 100 (0) Times
            [2] => 100 (2) Times
        )

    [2] => Array
        (
            [0] => 100 (1) Times
            [1] => 100 (0) Times
            [2] => 100 (2) Times
        )

    [3] => Array
        (
            [0] => 100 (1) Times
            [1] => 100 (0) Times
            [2] => 100 (2) Times
        )

)

Php demo

Sign up to request clarification or add additional context in comments.

Comments

0

You can use array_count_values( $array ) to get the count off all values on another array with the results.

example:

$count = array_count_values( $array );

echo "$a comes " . $count[$a] . ' times' ;

If you want to know only for one value just do an if.

example:

$count = array_count_values( $array );

if ( $a == 100 )
  echo "$a comes " . $count[$a] . ' times' ;

Edit:

More explained:

this is an array you want to count values.

$array = array(12,14,12,100,5,100);

then use array_count_values( ) to take the values on another array.

$count = array_count_values( $array );

Now you can do a loop to take all values.

foreach( $array as $a ){

  echo "$a comes " . $count[$a] . " times <br/>" ;

}

It will print:

12 comes 2 times
14 comes 1 times
12 comes 2 times
100 comes 2 times
5 comes 1 times
100 comes 2 times

You can change $count[$a] to $count[100] (out of the loop) to take the times that 100 is in array.

echo "100 comes " . $count[100] . " times" ;

Will print

100 comes 2 times

you can get more info here. https://www.php.net/manual/es/function.array-count-values.php

2 Comments

Array ( [0] => 0 ) Array ( [0] => 33 ) Array ( [0] => 44 ) Array ( [0] => 100 ) Array ( [0] => 33 ) Array ( [0] => 66 ) Array ( [0] => 100 ) Array ( [0] => 12 ); How to Count 100 using this array ?
@RohitYaduvanshi i updated the answer, hope help you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.