Jump to content

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


JustinK101

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.
Link to comment
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]
Link to comment
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
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.