Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:27:32+00:00 2026-06-11T16:27:32+00:00

Possible Duplicate: Search for PHP array element containing string I’ve created a mysql query

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:27:33+00:00Added an answer on June 11, 2026 at 4:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP MySQL multiple search query using option / select HTML form tags
Possible Duplicate: PHP, MySQL validation malfunction and search doesn't work? I have created a
Possible Duplicate: How to search for string in MS Access VBA array I am
Possible Duplicate: highlighting search results in php/mysql I am doing a search with a
Possible Duplicate: MySQL/PHP Search Efficiency How to Add FullText Capability to the existing table
Possible Duplicate: PHP MySQL Search And Order By Relevancy Hi, I have a table
Possible Duplicate: Search-like highlighting in NSTextView I thought i read here months ago that
Possible Duplicate: Do most browsers automatically search for the favicon.ico? Will a favicon.ico that
Possible Duplicate: Windows Phone 7 Search Button I think that all Windows Phone mobiles
Possible Duplicate: byte[] array pattern search I am trying to write a simple compression

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.