How to echo or print an array in PHP?

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

I have this array

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


        
相关标签:
11条回答
  • 2020-11-22 17:47

    You have no need to put for loop to see the data into the array, you can simply do in following manner

    <?php
    echo "<pre>";
     print_r($results); 
    echo "</pre>";
    ?>
    
    0 讨论(0)
  • 2020-11-22 17:51

    This will do

    foreach($results['data'] as $result) {
        echo $result['type'], '<br>';
    }
    
    0 讨论(0)
  • 2020-11-22 17:52

    Did you try using print_r to print it in human-readable form?

    0 讨论(0)
  • 2020-11-22 18:01

    To see the contents of array you can use.

    1) print_r($array); or if you want nicely formatted array then:

    echo '<pre>'; print_r($array); echo '</pre>';
    

    2) use var_dump($array) to get more information of the content in the array like datatype and length.

    3) you can loop the array using php's foreach(); and get the desired output. more info on foreach in php's documentation website:
    http://in3.php.net/manual/en/control-structures.foreach.php

    0 讨论(0)
  • 2020-11-22 18:01

    I checked the answer however, (for each) in PHP is deprecated and no longer work with the latest php versions.

    Usually we would convert an array into a string to log it somewhere, perhaps debugging or test etc.

    I would convert the array into a string by doing:

    $Output = implode(",", $SourceArray);
    

    Whereas:

    $output is the result (where the string would be generated

    ",": is the separator (between each array field

    $SourceArray: is your source array.

    I hope this helps

    0 讨论(0)
提交回复
热议问题