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 9210313
In Process

The Archive Base Latest Questions

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

I have data coming from a query and loading it into a 2 dimensional

  • 0

I have data coming from a query and loading it into a 2 dimensional array inside a loop.

I have the below code but when it prints out the array the index is missing.

How do I load a 2 dimensional aray with the first index being the $id value and second index being the $UserEmail and then how would I loop through the array to pull out each index ($id, $UserEmail)?

//the query
$query = "select id, UserEmail from User";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
    $ue = $row['UserEmail'];
    $id = $row['id'];
    if (strpos($ue,','))
    {
        $UserArrayEmail = explode(',',$ue);
        foreach ($UserEmailArray as $u)
        {
            $ArrayTerm[$id][] = $u;
        }
    }
}

//looping through the array and getting value from $id and $UserEmail
foreach( $ArrayTerm as $ArrayT ) {
    print_r($ArrayT);
    foreach( $ArrayT as $value ) {
        echo $value . "<br/>";
    }
}

Please help.

  • 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:02:46+00:00Added an answer on June 18, 2026 at 1:02 am

    mysql_connect() is deprecated your solution should use PDO instead.

    This is how it could be done with PDO and will fix the indexes:

    //fetch array from query 
    try { 
        $dbh = new PDO("mysql:host=$host; dbname=$dbname", $user, $pass); 
    } catch (PDOException $e) { 
        // db error handling 
    } 
    $sth = $dbh->prepare("select id, UserEmail from user"); 
    $sth->setFetchMode(PDO::FETCH_ASSOC); 
    $sth->execute(); 
    while($row = $sth->fetch())) { 
        $emails = $row['UserEmail']; 
        $id = $row['id']; 
        if (strpos($emails,',')) { 
             $UserEmailArray = explode(',',$emails); 
             foreach ($UserEmailArray as $email) { 
                 $ArrayT[$id][] = $email; 
             } 
        } 
    } 
    
    //repeat the same but with PDO data with other loops 
    
    }  
    

    Additionally, storing a list of data (in your case emails) in a single db column is not great for db normalization.

    You have no type safety (VARCHAR can containanything), no referential integrity, no way of actually processing the data with the db (in SELECTs, JOINs etc).
    For the db, the list of email addresses is just a bunch of random characters.

    The relational database model has a simple rule: one attribute, one value. So if you have multiple values, you have multiple rows. That’s what you should fix first. You’ll need another table named “user_email_addresses” or something.

    With this new table it could be something like:

    function html_escape($raw) { 
        return htmlentities($raw, ENT_COMPAT , 'utf-8'); 
    } 
    
    function log_exceptions($exception) { 
        echo $exception->getMessage(), '<br />', $exception->getTraceAsString();               
    } 
    set_exception_handler('log_exceptions'); 
    
    
    $database = new PDO( 'mysql:host=localhost;dbname=DB', 'USER', 'PASS', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) ); 
    
    $user_emails = array(); 
    $emails = $database->query(' 
        SELECT user_id , email 
        FROM user_email_addresses'); 
    
    foreach ($emails as $email) 
        $user_emails[ $email['user_id'] ][] = $email['email_address']; 
    
    //address list
    foreach ($user_emails as $user_id => $email_addresses) { 
        echo 'User: ' . html_escape($user_id) . '<br />'; 
        foreach ($email_addresses as $email_address) 
            echo html_escape($email_address) . '<br />'; 
        echo '<br />'; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 5 rows of data coming into my database every 2 hours from
I have following query in which data is coming from a table and in
I have some data from database coming out in a loop. Each of these
So I have some data that's coming back from a database query, and the
I have data coming from an external system (in CSV form). The data contains
I have data in this format coming from a database... BUS 101S Business and
I have couple of formulas and data coming from database. I want to refresh
I have a table in C#, the data is coming from an Excel file.
I have a stream of data coming in over the serial line from an
I have data coming into my Model, how do I setup to insert the

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.