#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    How to load two dimensional array


    I need to load a two dimensional array in a loop with data coming from a query. I tried the code below, but when the array prints out the first index is missing. So the question is how do I load a two dimensional array with the first index being the $ID value and the second index being the $HouseholdEMail and then how would I loop through the array to pull out each index ($ID, $HouseholdEMail)

    //load the array from the query
    $query = "select ID, HouseholdEMail from household";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
    $t = $row['HouseholdEMail'];
    $ID = $row['ID'];
    if (strpos($t,','))
    {
    $HouseholdEmailArray = explode(',',$t);
    foreach ($HouseholdEmailArray as $e)
    {
    $TermArray[$ID][] = $e;
    }
    }
    }

    //loop through the array and get value from both $ID and $HouseholdEmail
    foreach( $TermArray as $tArray ) {
    print_r($tArray);
    foreach( $tArray as $value ) {
    echo $value . "<br/>";
    }
    }
  2. #2
  3. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    1) please read the new user guide at the top of the forum and use the [ PHP ] tags to wrap your php code.

    2) mysql_connect() etc is deprecated, you should look at using PDO instead.

    3) this is how I would do it with pdo:

    PHP Code:
    //load the array from the query
    try {
        
    $dbh = new PDO("mysql:host=$host; dbname=$dbname"$user$pass);
    } catch (
    PDOException $e) {
        
    // do whatever error handling here
    }
    $sth $dbh->prepare("select ID, HouseholdEMail from household");
    $sth->setFetchMode(PDO::FETCH_ASSOC);
    $sth->execute();
    while(
    $row $sth->fetch())) {
        
    $emails $row['HouseholdEMail'];
        
    $ID $row['ID'];
        if (
    strpos($emails,',')) {
             
    $HouseholdEmailArray explode(',',$emails);
             foreach (
    $HouseholdEmailArray as $email) {
                 
    $TermArray[$ID][] = $email;
             }
        }
    }

    // the same so far but using PDO instead of old functions

    // your other loops would go here.

    4) notice my naming of the variables, you can see roughly what they do just by looking at them. Using really short names is fine in small scripts like this, but wait until you work on something big and need to figure out what $e is actually storing...

    5) Unless I am missing something, your code should do what you want it to. Maybe do a print_r($TermArray) after your while loop and see if it's got all the elements you expect.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    storing a list of data (in your case email addresses) in a single database column is wrong. See database normalization.

    Storing lists, "arrays" or whatever means that you circumvent the whole database systems. You have no type safety (a VARCHAR can contain anything), no referential integrity (doesn't apply in this case), no way of actually processing the data with the database system (like in SELECTs, JOINs etc). For the database system, your 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 "household_email_addresses" or something.

    PHP Code:
    <?php

    function html_escape($raw) {
        return 
    htmlentities($rawENT_COMPAT 'utf-8');
    }

    function 
    log_exceptions($exception) {
        echo 
    $exception->getMessage(), '<br />'$exception->getTraceAsString();                // CHANGE BEFORE PUTTING ONLINE
    }
    set_exception_handler('log_exceptions');


    $database = new PDO'mysql:host=localhost;dbname=YOURDB''YOURUSER''YOURPW', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) );

    $household_emails = array();
    $email_stmt $database->query('
        SELECT
            household_id
            , email_address
        FROM
            household_email_addresses
    '
    );
    foreach (
    $email_stmt as $email)
        
    $household_emails$email['household_id'] ][] = $email['email_address'];
        
    // list addresses
    foreach ($household_emails as $household_id => $email_addresses) {
        echo 
    'Household: ' html_escape($household_id) . '<br />';
        foreach (
    $email_addresses as $email_address)
            echo 
    html_escape($email_address) . '<br />';
        echo 
    '<br />';
    }

    Comments on this post

    • richpri agrees : Normalization is the first thing a database designer should be trained up on. It is the key to good design!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    San Jose
    Posts
    9
    Rep Power
    0
    In Javascript, there is no function that returns a two-dimensional array.

    Comments on this post

    • Jacques1 disagrees : What the hell are you talking about??
    • sir_drinxalot disagrees : thats great, but this is about PHP...

IMN logo majestic logo threadwatch logo seochat tools logo