array_diff between two different column names?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

array_diff between two different column names?

Post by JAB Creations »

I have two MySQL different tables with different column names however they reference the same thing: tag id's.

I'm unable to figure out how to get array_diff to work in the following way, here is a clean simple example of what I'm trying to do...

array_1 (tag id's being $_POST)
1,2,3,4,5,6,7

array_2 (tag id's that already exist in the database table)
1,2,3,4

array_diff (tag id's that don't yet exist and need to be added to the database table)
5,6,7

I'm trying to generate the array_diff in example of what I have above.

Here is the not so clean print_r example of what I have (PHP echo's this out)...
All Tags ID's: Array ( [0] => 1 [tag_id] => 1 ) , Array ( [0] => 2 [tag_id] => 2 ) , Array ( [0] => 3 [tag_id] => 3 ) , Array ( [0] => 4 [tag_id] => 4 ) , Array ( [0] => 5 [tag_id] => 5 ) , Array ( [0] => 6 [tag_id] => 6 ) , Array ( [0] => 7 [tag_id] => 7 )

Existing Tags: Array ( [0] => 1 [xhref_tag_id] => 1 ) , Array ( [0] => 2 [xhref_tag_id] => 2 ) , Array ( [0] => 3 [xhref_tag_id] => 3 )

Add Tags: Array ( [0] => 7 [tag_id] => 7 )
What do I need to do in order to get array_diff to work as desired?

The purpose of all of this is to determine what tags do not already have a record in the relational table for blog tags. Thoughts please?
Last edited by JAB Creations on Thu May 14, 2009 9:05 pm, edited 1 time in total.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: array_diff between two different column names?

Post by JAB Creations »

I just had the fancy idea of using MySQL's alias option however that didn't seem to work out.

I only just discovered mysql_data_seek earlier this afternoon however I'm not sure what a regular (non-MySQL) array would use? print_r and dump_var aren't helping and while it's been a long day I'm not ready to quit yet! :mrgreen:
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: array_diff between two different column names?

Post by JAB Creations »

I was stuck conceptually...and after having a gander at W3C School's list of array functions and not seeing anything directly useful I decided to go a different route.

Essentially what I've done was manually create each array item inside the while loop. I can't seem to figure out any method of comparing MySQL generated arrays directly however unless I missed a function or how it is supposed to be applied? Any way here is the code I was messing with.

Code: Select all

<?php
      mysql_data_seek($mysql_q03,0);
      echo '<div><b>All Tags ID\'s: </b>'; 
      $i = 1;
      //mysql_fetch_row
      while ($i <= $count && $row3 = mysql_fetch_array($mysql_q03))
      {
       print_r($row3['tag_id']);
       
       $tags3[$i] = $row3['tag_id'];
       
       if ($i < $count) {echo ', ';}
       $tags_add_existing[$i] = $row3['tag_id'];
       $i++;
      }
      echo '</div>';
 
 
      echo '<div><b>Existing Tags: </b>'; 
      $i = 1;
      //mysql_fetch_row
      while ($i <= $count && $row4 = mysql_fetch_array($mysql_q04))
      {
       print_r($row4['tag_id']);
       
       $tags4[$i] = $row4['tag_id'];
       
       if ($i < $count) {echo ', ';}
       $tags_add_existing[$i] = $row4['tag_id'];
       $i++;
      }
      echo '</div>';
 
      $tags_add = array_diff_assoc($tags3, $tags4);
      echo '<div><b>Add xhref relational Tags ID\'s: </b>'; 
      $counta = count($tags_add);
      while ($i <= $count)
      {
       echo $tags_add[$i];
       if ($i < $count) {echo ', ';}
       $i++;
      }
      echo '</div>';
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: array_diff between two different column names?

Post by Benjamin »

Here ya go Jab.

Code: Select all

 
<?php
function as_array_diff($a1, $a2) {
    $a3 = array();
 
    foreach ((array)$a1 as $v) {
        if (in_array($v, (array)$a2)) continue;
        $a3[] = $v;
    }
 
    return $a3;
}

print_r(as_array_diff(range(1, 10), range(5, 15)));
 
Normally, what I do is just delete them all and then add back what has been selected.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: array_diff between two different column names?

Post by JAB Creations »

Thanks Astions, that helped and it's appreciated. It's interesting to see how it's possible to do one thing several different ways. :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: array_diff between two different column names?

Post by VladSun »

Code: Select all

$a = Array(1,2,3,4,5,6,7);
$b = Array(1,2,3);
$c = array_diff($a, $b);
 
print_r($c);
=>

Code: Select all

Array
(
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
)
So, if you are going to use these result sets for finding their difference only, then rewrite your code in a way that will format result sets as arrays as shown above.
Also, I think you use mysql_fetch_array() and it's better to use some of the following:
mysql_fetch_assoc()
mysql_fetch_row()
or even mysql_fetch_array() but with its third argument int $result_type set to MYSQL_NUM or MYSQL_ASSOC

And finally ... why don't you do it in SQL ?
You need only a LEFT JOIN with IS NULL in the WHERE clause
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: array_diff between two different column names?

Post by JAB Creations »

Thanks VladSun. I think it was more effective to be able to show what I was doing once I finished the project...or at least to the point where I felt it was ready for critiquing. Please feel free to check it out here...
viewtopic.php?f=50&t=100533

I'm sure there will be plenty of feedback about my work! :lol:
Post Reply