Jump to content

[SOLVED] how does one compare 2 arrays?


elkidogz

Recommended Posts

ok, I'm going to have a select on two different tables, the difference i need to insert to a third, I can load the two up as arrays, but now i need compare the two arrays, and have the output entered into a third array so i can enter that one back into a table...

 

suggestions?

 

array 1 Touched files (select few from a HUGE of 5,000+ files)

array 2 all files (from same folder)

array 3 difference between the two

Link to comment
https://forums.phpfreaks.com/topic/65611-solved-how-does-one-compare-2-arrays/
Share on other sites

<?php

function insertarray($arrayName)
{
      foreach ($arrayName as $arrayElement)
            {
                  if ($arrayElement != '.' && $arrayElement != '..')// don't want . and .. in array just file names
                        {
                              $con = mysql_connect("my info");
                              if (!$con)
                                    {
                                          die('Could not connect: ' . mysql_error());
                                    }
                              mysql_select_db("DOH ", $con);
                              $query="INSERT INTO notadded (notinfilename) VALUES ('$arrayElement')";
                              $result = mysql_query($query);
                              echo "File:".$arrayElement." was added to the table. <br>";
                        }
             }
}
$files = scandir("allPICS",0);
insertarray($files);
?>

 

Sorry but now, i'm having issues with this insert line, basically it only inserts 30 at a time, how can i make it continue? with a counter while loop or something? make it sleep? please help there are roughly 3000-5000 records in this folder all of them need to go into a table so i can compare them .

because i have one table where people are going to be working on these files that will constantly be added to the folder, it never shrinks, it only gets bigger...

 

the first table is the work done table they are adding information about a picture to a table that has the file name, like a title, notes, etc...

the second table is just a dump of the files that are in the folder.

a third table is being generated so that i can display the pictures that are in the folder but don't have any information on them yet...

   <?php 
function insertarray($arrayName)
{
      foreach ($arrayName as $arrayElement)
            {
                  if ($arrayElement != '.' && $arrayElement != '..')// don't want . and .. in array just file names
                        {
                              $con = mysql_connect("my info");
                              if (!$con)
                                    {
                                          die('Could not connect: ' . mysql_error());
                                    }
                              mysql_select_db("DOH ", $con);
                              $query="INSERT INTO notadded (notinfilename) VALUES ('$arrayElement')";
                              $result = mysql_query($query);
                              echo "File:".$arrayElement." was added to the table. <br>";
                        }
             }
}
function comparearrays()
{   
      $query1="SELECT notinfilename FROM notadded";
      $result1 = mysql_query($query1);
      $query2="SELECT  fileName FROM pictures";
      $result2 = mysql_query($query1);
      $result = array_diff($result1, $result2);
      $query="INSERT INTO tobeadded (tobeaddedfilename) VALUES ('$result')";
      return($result);

}
$files = scandir("allPICS",0);
insertarray($files);
comparearrays();
printr $result; //wanna see the results
?>

Never mind the above code... i've updated it since then...

 

<?php
include('Gallery.php');

function insertarray($arrayName)
{
      $con = mysql_connect("no no");
      if (!$con)
            {
                  die('Could not connect: ' . mysql_error());
            }
      mysql_select_db("nope", $con);
      $cleartable="TRUNCATE TABLE `notadded`"; //clears table notadded
      $doitnow=mysql_query($cleartable);
      foreach ($arrayName as $arrayElement)//rebuilds table notadded
            {
                  if ($arrayElement != '.' && $arrayElement != '..')// don't want . and .. in array just file names
                        {
                              $query="INSERT INTO notadded (notinfilename) VALUES ('$arrayElement')";
                              $result = mysql_query($query);
                              echo "File:".$arrayElement." was added to the table notadded. <br>";
                        }
             }

}
function comparearrays()
{

      $con = mysql_connect("no no");
      if (!$con)
            {
                  die('Could not connect: ' . mysql_error());
            }
            mysql_select_db("nope", $con);
      $cleartable="TRUNCATE TABLE `tobeadded`"; //clears table tobeadded
      $doitnow=mysql_query($cleartable);
      $query1="SELECT notinfilename FROM notadded";
      $result1 = mysql_query($query1);
      $query2="SELECT  fileName FROM pictures";
      $result2 = mysql_query($query1);
      $result = array_diff($result1, $result2);
      foreach ($result as $arrayElement)//rebuilds table tobeadded
            {
                  $query="INSERT INTO tobeadded (tobeaddedfilename) VALUES ('$arrayElement')";
                  $doit = mysql_query($query);
                  echo "File:".$arrayElement." was added to table tobeadded";
            }
      echo "done";
}
$files = scandir("allPICS",0);
insertarray($files);
comparearrays();
mysql_close($con)
?>

 

it goes into both functions, but it only performs the first ones insert am i not doing the arrays right?... what's going on here?

Archived

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

×
×
  • 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.