PHP - count all array elements satisfying the condition

Possible duplicate:
Search for a PHP array element containing a string

I created a MySQL query that scans several products, all with the following information:

Product ID Product Name Product Price and Product Category

Further down the page, I went over them with foreach and a few ifs, so it only displays products in which the name contains "x" in one div, and displays those products in which the name contains "y" in another div.,

I'm struggling to figure out how many products will be in each div before I do a loop.

Essentially, I ask:

What do you think of all the elements in the array that satisfy a certain condition?

Added code that 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 would like to know how many of them will be here before I actually do the loop.

+4
source share
2 answers

Well, without seeing the code, so generally speaking, if you are going to separate them anyway, can you also do this in advance?

 <?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 pay attention to the comment, which says: β€œThis is usually faster in SQL,” because it is a more sensible approach if you want to filter the values.

EDIT : Changed the variable name to adapt the variable names in the sample code.

+4
source

Use array filter: http://www.php.net/manual/en/function.array-filter.php

 array array_filter ( array $input [, callable $callback = "" ] ) 

Iterates over each value in the input array, passing them the callback functions. If the callback function returns true, the current value from the input is returned to the result array. Array keys are saved.

 <?php function odd($var) { // returns whether the input integer is odd return($var & 1); } function even($var) { // returns whether the input integer is even return(!($var & 1)); } $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array(6, 7, 8, 9, 10, 11, 12); echo "Odd :\n"; print_r(array_filter($array1, "odd")); echo "Even:\n"; print_r(array_filter($array2, "even")); ?> 

But keep in mind that this is a loop, but your SQL query will be faster.

+2
source

Source: https://habr.com/ru/post/1435485/


All Articles