Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 07 April 2006 - 06:36 PM

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.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 April 2006 - 06:49 PM

$result = array_diff($array1, $array2);

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 07 April 2006 - 07:14 PM

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.

$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);
        }

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

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

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 April 2006 - 07:18 PM

What are you going to do with the results of the query?
<?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>';
        }
?>

Ken

#5 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 07 April 2006 - 07:25 PM

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.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 April 2006 - 07:38 PM

You could build a query like that, but if you have thousands of entries, you will end up with a very long query.
<?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
//
?>

Doing it the original way is equivalent to stringing the "OR" clauses
<?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
//
?>

Ken

#7 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 07 April 2006 - 09:28 PM

Ken,

Thanks, yeah the implode worked well.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users