SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    display this array

    Very simple i would think. I just have never dealt with arrays in this way.

    $thisArray is the array variable

    this is what print_r gives me:

    Array ( [last] => Daniel [first] => Adam )

    do i need to use a for loop, or a while loop.

    I believe to utilize a for loop the array column names such as 'last' and 'first' would need to be numbers. so i don't think that is the way i need to go.

    I have noticed that there is a plethera of information out there about very in depth issues dealing with php, but when it comes really very simple stuff i can't find anything. and what i do find (ie. PHP Manual) confuses the crap out of me.

    so if anyone has any suggestions for super basic tutorials please let me know.

    I have been through both yank's and fueks' books, and they have been very helpful. there are just a hand full of holes in php that i do not understand yet.

    thanks

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use foreach
    PHP Code:
    foreach( $thisArray as $key => $value )
    {
       
    // $key would be 'last' or 'first'
       // $value would be 'Daniel' or 'Adam'

    You can also do this with a while loop, but it's a bit more syntax and function calls
    PHP Code:
    while( list( $key$value ) = each$thisArray ) )
    {
        
    // same thing

    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    use foreach
    PHP Code:
    foreach( $thisArray as $key => $value )
    {
       
    // $key would be 'last' or 'first'
       // $value would be 'Daniel' or 'Adam'

    You can also do this with a while loop, but it's a bit more syntax and function calls
    PHP Code:
    while( list( $key$value ) = each$thisArray ) )
    {
        
    // same thing

    Ok That works awsome, now is there a way i can convert that array to a string?

    Thanks a ton!

  4. #4
    SitePoint Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you mean one string for all names, do this...

    PHP Code:
    $thisString ''// stops us from getting a notice

    foreach( $thisArray as $key => $value ) { 
        
    $thisString .= $key ' : '$value "\n";
    }

    echo 
    $thisString
    or if you want to store the array somewhere...

    PHP Code:
    $serializedString serialize($thisArray); 

  5. #5
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THat works great. now can you tell me if i am doing something wrong in this?

    PHP Code:
    function thisFunction(& $thisArray) {
            
    $thisString ''// stops us from getting a notice 

            
    foreach( $thisArray as $key => $value ) { 
                
    $thisString .= $key " "
            } 
            echo(
    $thisString);


    Cause i get this error:

    Warning: Invalid argument supplied for foreach() in c:filename
    on line 247

    I am sending to the function the same array that this little foreach statement worked with before sending to the function.

    any idea why this might happen

  6. #6
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no worries.....

    I figured it out, at least for now.

    Thanks guys


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
  •