Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976132
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:46:04+00:00 2026-05-20T04:46:04+00:00

I have two arrays in two MYSQL tables. I want to loop through the

  • 0

I have two arrays in two MYSQL tables. I want to loop through the first array (array1), if a value exists in the second array (array2), skip that value in the second array (array2) and continue checking. If it doesn’t exists, add it to the second array (array2).

An example:

   array1 = 1,2,3,4,5
   array2 = 2,4,6,8,10

So we loop over array1 to check if an element exists in array2. if it doesn’t add it to array 2. Therefore, the new value of array2 should be:

   array2= 1,2,3,4,5,6,8,10

This is what I have done and it doesn’t work. It deletes all the value of array2 and adds the new value. It doesn’t ignore the common values. Please help.

    $array1= explode(",", $results1);
$array2= explode(",", $results2);
foreach($array1 as $key => $value) {
    if (in_array($value, $array2)) {
        if ($results2 != "") { 
            $results2= "$results2,$value";
        } else { $results2= "$value"; }
          mysql_query("UPDATE datas SET results2 ='$results2' 
                    WHERE r2_id ='$r2_id'")
                    or die ("Died".mysql_error());
    } else {
        //Do nothing
    }
}

Is there anything i’m doing wrong? Please help. I have been at this for a while now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:46:05+00:00Added an answer on May 20, 2026 at 4:46 am

    Here’s a one-liner that merges the arrays:

    $merged = array_unique(array_merge(explode(",", $results1), explode(",", $results2)));
    $string = implode(",", $merged);
    
    mysql_query("UPDATE datas SET results2 = '$string' WHERE r2_id = $r2_id") or die("Died ".mysql_error());
    

    As far as what you’re doing wrong, since you asked:

    • Your first conditional is backwards. If the value is already in array 2, you want to do nothing, instead you’re doing nothing when it’s missing and should be added.
    • You make a lot of use of double quoted strings when concatenation would be cleaner (easier to read and harder to make unnoticed mistakes).
    • You issue an UPDATE query every time you change $results2 when you only need to update it once, after building the whole string.
    • You quote what appear to be numeric values in the query ($r2_id). MySQL won’t complain about it, but you might be surprised when the implicit conversion from a string to a number doesn’t yield the result you expected.
    • You don’t need an else clause if there’s nothing in it.
    • Storing a serialized array in a column of a database table in the first place is usually a wrong thing to do.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
that´s my first question in stackoverflow . I have two MYSQL tables: categories and
I have two set of arrays in different MYSQL table. This what I want
I have two tables in mySQL, notably accounts and subscriber_data. I want to use
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays built while parsing a text file. The first contains the
I have two tables with list of urls fetched from different sources. I want
I have two tables in a MySql DB: items id (char) name (varchar) description
using mysql and php, I have two tables, I'm french so the table names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.