How to echo or print an array in PHP?

前端 未结 11 1547
春和景丽
春和景丽 2020-11-22 17:18

I have this array

Array
(
  [data] => Array
    (
      [0] => Array
        (
          [page_id] => 204725966262837
          [type] => WEBSITE         


        
11条回答
  •  半阙折子戏
    2020-11-22 17:43

    There are multiple function to printing array content that each has features.

    print_r()

    Prints human-readable information about a variable.

    $arr = ["a", "b", "c"];
    
    echo "
    ";
    print_r($arr);
    echo "
    ";
    Array
    (
        [0] => a
        [1] => b
        [2] => c
    )
    


    var_dump()

    Displays structured information about expressions that includes its type and value.

    echo "
    ";
    var_dump($arr);
    echo "
    ";
    array(3) {
      [0]=>
      string(1) "a"
      [1]=>
      string(1) "b"
      [2]=>
      string(1) "c"
    }
    


    var_export()

    Displays structured information about the given variable that returned representation is valid PHP code.

    echo "
    ";
    var_export($arr);
    echo "
    ";
    array (
      0 => 'a',
      1 => 'b',
      2 => 'c',
    )
    

    Note that because browser condense multiple whitespace characters (including newlines) to a single space (answer) you need to wrap above functions in

     to display result in correct format.


    Also there is another way to printing array content with certain conditions.

    echo

    Output one or more strings. So if you want to print array content using echo, you need to loop through array and in loop use echo to printing array items.

    foreach ($arr as $key=>$item){
        echo "$key => $item 
    "; }
    0 => a 
    1 => b 
    2 => c 
    

提交回复
热议问题