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

  • Home
  • SEARCH
  • 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 9214449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:58:33+00:00 2026-06-18T01:58:33+00:00

I have two arrays both are coming from my SQL server Example: Array 1

  • 0

I have two arrays both are coming from my SQL server

Example:
Array 1 (45, 46, 47, 48)
Array 2 (46, 47)

My code is as follows:

<select>
   <?
      while($array1 = $t->fetch_object()) /*Get from DB*/ {
        foreach($array2 as $a2){
   ?>
       <option <?=$array1 == $a2 ? 'value="'.$array1'" select="selected"' : 'value="'.$array1.'"'?>><?=$array1?><option>
<? } }?>
</select>

This will produce itself multiple times as it loops array2 twice. I for some reason can’t figure out how to run this properly without the multiple options. (Maybe I have been working all day) But any help will definitely be appreciated.

EDIT: so instead of the output being:

45
46 selected
47 selected
48
45 selected
46 selected
47
48

It be:
45
46 selected
47 selected
48

The above loops through twice due to the foreach and I’m not sure how to have it just loop through once. Hmm maybe if I have $i = 1 and it stop at the next count.

To clarify I am using this http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/ dropdown menu.

  • 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-06-18T01:58:34+00:00Added an answer on June 18, 2026 at 1:58 am

    I might don’t understand well your question, but what I see from the code. You are trying to make default selection from array2. My way to do this would be :

    <select>
    <?php
    //Here comes the php
    
    //Variables
    //---------------
    $array2 = array(46,47);
    //---------------
    
    //Looping into the fecthed data
    //------------------
    while($array1 = $t->fetch_object()) /*Get from DB*/ {
        //Initalization of looping variables
        $boolFound = false;
        $i = 0;
    
        //looking into aray2 for default values
        while($boolFound == false && $i < count($array2)) //While a default is not a match 
        {                                                 //or the end of array2
            if($array2[$i] == $array1) //Is this a default value?
            {
                 $boolFound = true; //Yes, we stop the loop
            }
            else
            {
                $i++; //No, we continu until the end of array2
            }
        }
    
        //Either we have a default or not we output the option
        echo('<option value="'.$array1.'" ');
    
        //if it is a default, we output "selected"
        if($boolFound)
        {
            echo('select="selected"');  
        }
    
        //Then the value itself, and we close the html
        echo('>'.$array1.'</option>');
    }
    //------END of data looping------------
    //END of PHP    
    ?> 
    </select>
    

    The problem with this. You may have more than 1 “default” option (selected).

    Hope this help.

    antoine

    (also I am a little rusty in php, not sure about “$array1 = $t->fetch_object()” part.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays. Now i want to compare some elements in from both
I have two arrays. Example of the first array: $arrayOne = array ( 'fruit'
I have two string arrays Array1[size] and Array2[size]. They both have the same size.
I have two arrays, both have the same keys (different values) however array #2
I have two arrays that both look like this: Array ( [0] => Array
I have two arrays. I'd like to copy ranges of data from one of
I have two arrays... $Name = array(a, b, c, d); $Value = array(1, 2,
I have two arrays, N and M. they are both arbitrarily sized, though N
I have two arrays, $A and $B. Both arrays have the same length, but
I have two arrays. One is named foods, it is an array of strings.

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.