Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Count all elements of an Array that Satisfy a Condition [duplicate]

Tags:

arrays

php

Possible Duplicate:
Search for PHP array element containing string

I've created a mysql query that pulls through several products, all with the following information:

Product ID Product Name Product Price and Product Category

Further down the page, I've looped through these with a foreach and a few 'ifs' so it only displays those products where the name contains 'x' in one div and displays those products where the name contains 'y' in another div.

I'm struggling to count how many products are going to be in each div before I do the loop.

So essentially, what I'm asking is:

How do you count all elements in an array that satisfy a certain condition?

Added Code which shows the loop:

<div id="a">
    <?php
    $i = 1;
    foreach ($products AS $product) {
        if (strpos($product->name,'X') !== false) {
            =$product->name
        }
        $i++;
    } ?>
</div>

<div id="b">
    $i = 1;
    foreach ($products AS $product) {
        if (strpos($product->name,'Y') !== false) {
            =$product->name
        }
        $i++;
    } ?>
</div>

I'd like to know how many of these are going to be in here before I actually do the loop.

like image 395
Daniel Kilburn Avatar asked Sep 21 '12 11:09

Daniel Kilburn


People also ask

How do you count the number of repeated elements in an array in PHP?

The array_count_values() function returns an array with the number of occurrences for each value. It returns an associative array. The returned array has keys as the array's values, whereas values as the count of the passed values.

How do I count items in an array PHP?

You can simply use the PHP count() or sizeof() function to get the number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that has been initialized with an empty array, but it may also return 0 for a variable that isn't set.

What is the use of Array_count_values () in PHP explain with example?

The array_count_values() function is used to count all the values inside an array. In other words, we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Parameters: This function accepts a single parameter $array.


1 Answers

Well, without seeing the code, so generally speaking, if you're going to split them anyway, you might as well do that up-front?

<?php
// getting all the results.
$products = $db->query('SELECT name FROM foo')->fetchAll();

$div1 = array_filter($products, function($product) {
    // condition which makes a result belong to div1.
    return substr('X', $product->name) !== false;
});

$div2 = array_filter($products, function($product) {
    // condition which makes a result belong to div2.
    return substr('Y', $product->name) !== false;
});

printf("%d elements in div1", count($div1));
printf("%d elements in div2", count($div2));

// then print the divs. No need for ifs here, because results are already filtered.
echo '<div id="a">' . PHP_EOL;
foreach( $div1 as $product ) {
   echo $product->name;
}
echo '</div>';

echo '<div id="b">' . PHP_EOL;
foreach( $div2 as $product ) {
   echo $product->name;
}
echo '</div>';

That being said: you should take notice of the comment which says "This is normally faster in SQL", because it is the more sane approach if you want to filter the values.

EDIT: Changed the name of the variables to adapt the variable names in the example code.

like image 91
Berry Langerak Avatar answered Sep 19 '22 18:09

Berry Langerak