Jump to content

Archived

This topic is now archived and is closed to further replies.

JustinK101

If the element in $array_1 is NOT in $array_2 then push that element onto another new array $result??????

Recommended Posts

Hello,

I have two full arrays of varied size:

$array_1
$array_2

How can I write code to compare every element in $array_1 to $array_2?

I need to do: If the element in $array_1 is NOT in $array_2 then push that element onto another new array $result. Clearly then, if the element in $array_1 is in $array_2 then do nothing, i.e. do not push onto $result.

Thanks for the advice.

Share this post


Link to post
Share on other sites
[code]
$result = array_diff($array1, $array2);
[/code]

Share this post


Link to post
Share on other sites
Ok now I need to run a querry on each serial in the array result. I have code, but I am certain its not going to work.

[code]
$db_name = "yjpalmer_inventoryManager";
        mysql_select_db($db_name);
            
        foreach($array_result as $current_serial)
        {
            $sql_3 = "SELECT id, type_of_device, serial, condition_of_device, associated_order_id, associated_customer_name, UNIX_TIMESTAMP(date_shipped) as sdate FROM inventory WHERE isAssociated = 'Yes' AND serial = '" . $current_serial . "' ORDER BY " . $sort . " " . $ascendOrDescend . "";
            $result_3 = mysql_query($sql_3);
        }
[/code]

How do I build up an array of these results in a row to output?

[b]Acutally I think I may need to do AND serial = array_result[0] OR serial = array_result[1] OR serial = array_result[2] ....[/b]

Share this post


Link to post
Share on other sites
What are you going to do with the results of the query?
[code]<?php
        foreach($array_result as $current_serial)
        {
            $sql_3 = "SELECT id, type_of_device, serial, condition_of_device, associated_order_id, associated_customer_name, UNIX_TIMESTAMP(date_shipped) as sdate FROM inventory WHERE isAssociated = 'Yes' AND serial = '" . $current_serial . "' ORDER BY " . $sort . " " . $ascendOrDescend . "";
            $result_3 = mysql_query($sql_3);
//
//   you need to fetch the results and do something with them
//
            $row = mysql_fetch_assoc($result_3);
            $echo '<pre>' . print_r($row,true) . '</pre>';
        }
?>[/code]

Ken

Share this post


Link to post
Share on other sites
Ken,

Yeah I display a table and output, but I dont want to do all my HTML code in the foreach loop. I think I need to run only 1 sql query and build up my query like:

... WHERE serial = '" . $result[0] . "' OR serial = '" . $result[1] . "' OR serial = '" . $result[2] . "' OR serial = '" . $result[X] . "' ...


What is the best way of doing this? My result array size isunknown so I cant just write out each combination, plus there are AROUND a few thousand elements in the array so that would'nt be fun.

Share this post


Link to post
Share on other sites
You could build a query like that, but if you have thousands of entries, you will end up with a very long query.
[code]<?php
//
// output the start of your table
//
$sql_3 = "SELECT id, type_of_device, serial, condition_of_device, associated_order_id, associated_customer_name, UNIX_TIMESTAMP(date_shipped) as sdate FROM inventory WHERE isAssociated = 'Yes' AND serial = '" . implode("' OR serial = '", $result) . "' ORDER BY " . $sort . " " . $ascendOrDescend . "";
$result_3 = mysql_query($sql_3);
while ($row = mysql_fetch_assoc($result_3))
{
//
// output a row of your table
//
}
//
// output the end of your table
//
?>[/code]

Doing it the original way is equivalent to stringing the "OR" clauses
[code]<?php
//
// output the start of your table
//
        foreach($array_result as $current_serial)
        {
            $sql_3 = "SELECT id, type_of_device, serial, condition_of_device, associated_order_id, associated_customer_name, UNIX_TIMESTAMP(date_shipped) as sdate FROM inventory WHERE isAssociated = 'Yes' AND serial = '" . $current_serial . "' ORDER BY " . $sort . " " . $ascendOrDescend . "";
            $result_3 = mysql_query($sql_3);
            $row = mysql_fetch_assoc($result_3);
//
// output a row of your table
//
        }
//
// output the end of your table
//
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.