SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy implode function not working

    I first posted this in the javascript forum, but it turned to be a php problem. When I try to implode the $myarray I get an "Array to string conversion" error. If I change my array in the implode function to myarray2 it works. How can I get the implode function to work with myarray? I'm not sure why it doesn't work with that one, thanks.

    Code:
    $x = 1;
    while($row = mysql_fetch_array($subcategory)) {
            $subid = $row['subid'];
            $subcat = $row['subcategory'];
            $myarray[$x] = array($subid, $subcat); 
    	$x = $x + 1;
    } 
    print_r($myarray);
    $myarray2 = array('test', 'test2', 'test3');
    print_r($myarray2);
    ?>
    
    <?php 
    $test = implode(', ', $myarray);
    echo($test);
     ?>

  2. #2
    SitePoint Evangelist nsj's Avatar
    Join Date
    Oct 2005
    Location
    Jamaica (W.I)
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $myarray2 works fine as it's a regular array with 3 elements however $myarray is a multidimenSional array.
    The correct way to do this is:
    PHP Code:
    $str "";
    for(
    $i=0$i<count($myarray); $i++){
         
    $str .= implode(", ",$myarray[$i]);
    }
    print 
    $str#or whatever. 
    Cheers!

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried cutting and pasting your code but it gives me the following error

    Code:
    Undefined offset: 0 in C:\WebSite\tutform.php on line 49
    
    Warning: implode() [function.implode]: Bad arguments. in C:\WebSite\tutform.php on line 49

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found out what the problem was. I set the first array at x=1, and your code started at 0, since array(0) didn't exist it caused that error. Thanks for the code I can finally implode stuff


Bookmarks

Posting Permissions

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