the way of output an array's variable

Discussion in 'Programming' started by runeveryday, Jul 27, 2009.

  1. #1
    how many ways we can do to output an array's variable,
    eg:$row=array{...}

    as far as i know,the followings are the output format,which is right?and why?

    echo $row[$key];
    echo $row[key];
    echo $row['key'];
    i have tested the echo $row[$key]; is wrong, and what is the difference of the rest.thank you!
     
    runeveryday, Jul 27, 2009 IP
  2. runeveryday

    runeveryday Active Member

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    who can explain?
    thank you
     
    runeveryday, Jul 30, 2009 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    print_r($row);
     
    Kaizoku, Jul 30, 2009 IP
  4. yellow1912

    yellow1912 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you want nicer output to debug, try the function below (not written by me btw)

    function print_nice($elem,$max_level=10,$print_nice_stack=array()){
        if(is_array($elem) || is_object($elem)){
            if(in_array(&$elem,$print_nice_stack,true)){
                echo "<font color=red>RECURSION</font>";
                return;
            }
            $print_nice_stack[]=&$elem;
            if($max_level<1){
                echo "<font color=red>nivel maximo alcanzado</font>";
                return;
            }
            $max_level--;
            echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>";
            if(is_array($elem)){
                echo '<tr><td colspan=2 style="background-color:#333333;"><strong><font color=white>ARRAY</font></strong></td></tr>';
            }else{
                echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
                echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></td></tr>';
            }
            $color=0;
            foreach($elem as $k => $v){
                if($max_level%2){
                    $rgb=($color++%2)?"#888888":"#BBBBBB";
                }else{
                    $rgb=($color++%2)?"#8888BB":"#BBBBFF";
                }
                echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
                echo '<strong>'.$k."</strong></td><td>";
                print_nice($v,$max_level,$print_nice_stack);
                echo "</td></tr>";
            }
            echo "</table>";
            return;
        }
        if($elem === null){
            echo "<font color=green>NULL</font>";
        }elseif($elem === 0){
            echo "0";
        }elseif($elem === true){
            echo "<font color=green>TRUE</font>";
        }elseif($elem === false){
            echo "<font color=green>FALSE</font>";
        }elseif($elem === ""){
            echo "<font color=green>EMPTY STRING</font>";
        }else{
            echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem);
        }
    }
    PHP:
     
    yellow1912, Jul 30, 2009 IP
  5. ridem

    ridem Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    And for nice formating in browserL

    
    echo "<pre>";
    print_r($row);
    echo "</pre>";
    
    PHP:
     
    ridem, Aug 2, 2009 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    can just view source :)
     
    Kaizoku, Aug 2, 2009 IP
  7. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    echo $row[$key]; doesn't make sense, it's supposed to be used for assignment only when you want to force a key for an array value, e.g. instead of seeing Array([0] => 'blah') you would see Array([keyval] => 'blah').

    echo $row[key]; works, just uses slightly more memory than using

    echo $row['key']; ;)
     
    szalinski, Aug 12, 2009 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    Please learn more php.
     
    Kaizoku, Aug 13, 2009 IP