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!
This is usually done in SQL
if you want whole row that has at least 1 matching SKU in another table:
If you want only SKUs
In PHP intersect variant you need to build the arrays separately and then use
array_intersect