Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts

    loop turning output into double array

    I am trying to output each row as an array to then export a CSV. I have the code working however it is outputting two of each column value. I even know why it is outputting two, because the array created is listing the value as both indexed and numbered arrays. I dont know why it is doing this!

    PHP Code:
    // Get profiles that were selected  
    $result_2 mysql_query("SELECT {$query_ready} FROM all_child LEFT JOIN {$sql_instance}child ON {$sql_instance}child.{$sql_instance}child_id = all_child.child_id") or die(mysql_error());
    while(
    $row mysql_fetch_array($result_2)){
        
    $loop[] = $row;
    }



    foreach( 
    $loop as $row )  
    {  
        
    fputcsv($outstream$row',''"');  
    }  
      
    fclose($outstream);  ?> 
    With a print_r I can see the problem,

    PHP Code:
    Array ( [0] => Array ( [0] => Array ( [0] => 1000001 [account_id] => 1000001 [1] => 10000007 [child_id] => 10000007 
    As you can see account_id is both [0] and [account_id] which is outputting two account ID values (and every other value) in my CSV file! Any help much appreciated
    Last edited by itxtme; 08-19-2012 at 12:20 AM.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    mysql_fetch_array fetches as both a numeric and associative array. Try mysql_fetch_row which uses a numeric (enumerated) array.

    I'm curious why so many people are still using mysql rather than the (recommended) mysqli? Perhaps there is so much legacy code out there?!
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Fantastic, that worked like a charm, and seems obvious now that I look back at it! I think you are right, I learnt the original and have stuck to it. I will however take a look at the newer mysqli


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •