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

  • Home
  • SEARCH
  • 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 8670183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:39:11+00:00 2026-06-12T18:39:11+00:00

this is my first time posting here, although I have gotten many great tips

  • 0

this is my first time posting here, although I have gotten many great tips and techniques reading the posts here.

Here is my objective:

I have 2 somewhat similar tables to compare. For each row of each table, I am pulling the fields I want into into an array.

I basically want to echo out the values of any array from one table that has matching values in the other array.

Here is my code, maybe it will be easier to understand.

$sql = "SELECT * FROM $i_comp ORDER BY `manufacturer`";
$statement = $objDb->query($sql);
$c_skus = $statement->fetchAll(PDO::FETCH_ASSOC);

$sql = "SELECT `sku_one`,`sku_two`,`qty`,`manufacturer`";
$sql .= "FROM $i_gas ORDER BY `manufacturer`";
$statement = $objDb->query($sql);
$d_skus = $statement->fetchAll(PDO::FETCH_ASSOC);

foreach ( $c_skus as $c_sku ) {
    // i want to see if any values of this array exist in the array created hy
    // the foreach loop below (yes, repeat for each row)
    $c = array($c_sku['sku'],$c_sku['sku_one'],$c_sku['sku_two'],$c_sku['sku_three']);
    foreach ( $d_skus as $d_sku ) {
        $d = array($d_sku['sku_one'],$d_sku['sku_two']);
        $intersect = array_intersect($c, $d);
        echo '<pre>', print_r($intersect), '</pre>';
    }
}

Here are the results I receive for each iteration of the code:

Array
(
)
1

It should also be noted that I am not concerned with the Keys, just the Values. Eventually, those values will be works into an INSERT statement but for the time being I just need to get the right results.

Anyways, thanks for any and all help!

  • 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-12T18:39:12+00:00Added an answer on June 12, 2026 at 6:39 pm

    This is usually done in SQL

    if you want whole row that has at least 1 matching SKU in another table:

    $sql = "
    SELECT c.* FROM $i_comp AS c 
      JOIN $i_gas AS d 
        ON d.sku_one in (c.sku, c.sku_one, c.sku_two, c.sku_three) 
        OR d.sku_two in (c.sku, c.sku_one, c.sku_two, c.sku_three) 
     ORDER BY c.`manufacturer`";
    $statement = $objDb->query($sql);
    $c_skus = $statement->fetchAll(PDO::FETCH_ASSOC);
    // all rows that have at least 1 matching sku on another table
    print_r($c_skus);
    

    If you want only SKUs

    $sql = "
    SELECT d.sku_one FROM $i_comp AS c 
      JOIN $i_gas AS d 
        ON d.sku_one in (c.sku, c.sku_one, c.sku_two, c.sku_three) 
    UNION
    SELECT d.sku_two FROM $i_comp AS c 
      JOIN $i_gas AS d 
        OR d.sku_two in (c.sku, c.sku_one, c.sku_two, c.sku_three) 
     ";
    $statement = $objDb->query($sql);
    $c_skus = $statement->fetchAll(PDO::FETCH_ASSOC);
    // all skus that have at least 1 matching sku on another table
    print_r($c_skus);
    

    In PHP intersect variant you need to build the arrays separately and then use array_intersect

    $c = array();
    foreach ( $c_skus as $c_sku ) {
        $c[] = $c_sku['sku'];
        $c[] = $c_sku['sku_one'];
        $c[] = $c_sku['sku_two'];
        $c[] = $c_sku['sku_three'];
    }
    $d = array();
    foreach ( $d_skus as $d_sku ) {
        $d[] = $d_sku['sku_one'];
        $d[] = $d_sku['sku_two'];
    }
    
    $intersect = array_intersect($c, $d);
    echo '<pre>', print_r($intersect), '</pre>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first time posting here, I have a question which I have
this is my first time posting on here and have read alot of helpful
This is my first time posting on Stackoverflow, but I've been reading through many
First time posting here, will try to be succinct. This is a classic 'can't
this is my first time posting here, but I didn't know where else to
This is my first time posting a question here, it has been a valuable
So this is my first time posting here but I'm more or less stumped.
First time posting here after having great results searching for other answers on stackoverflow.
This is my first time posting here, so I apologize in advance for the
This is my first time posting here, but I've found a lot of answers

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.