Display an array in a readable/hierarchical format

后端 未结 18 953
北荒
北荒 2020-12-02 04:53

Here is the code for pulling the data for my array



        
相关标签:
18条回答
  • 2020-12-02 05:26

    I assume one uses print_r for debugging. I would then suggest using libraries like Kint. This allows displaying big arrays in a readable format:

    $data = [['Natural Child 1', 'Natural Child 2', 'Natural Child 3']];
    Kint::dump($data, $_SERVER);
    

    0 讨论(0)
  • 2020-12-02 05:28

    Instead of

    print_r($data);
    

    try

    print "<pre>";
    print_r($data);
    print "</pre>";
    
    0 讨论(0)
  • 2020-12-02 05:28

    This tries to improve print_r() output formatting in console applications:

    function pretty_printr($array) {
    
      $string = print_r($array, TRUE);
    
      foreach (preg_split("/((\r?\n)|(\r\n?))/", $string) as $line) {
    
        $trimmed_line = trim($line);
    
        // Skip useless lines.
        if (!$trimmed_line || $trimmed_line === '(' || $trimmed_line === ')' || $trimmed_line === 'Array') {
          continue;
        }
    
        // Improve lines ending with empty values.
        if (substr_compare($trimmed_line, '=>', -2) === 0) {
          $line .=  "''";
        }
    
        print $line . PHP_EOL;
      }
    }
    

    Example:

    [activity_score] => 0
    [allow_organisation_contact] => 1
    [cover_media] => Array
            [image] => Array
                    [url] => ''
            [video] => Array
                    [url] => ''
                    [oembed_html] => ''
            [thumb] => Array
                    [url] => ''
    [created_at] => 2019-06-25T09:50:22+02:00
    [description] => example description
    [state] => published
    [fundraiser_type] => anniversary
    [end_date] => 2019-09-25
    [event] => Array
    [goal] => Array
            [cents] => 40000
            [currency] => EUR
    [id] => 37798
    [your_reference] => ''
    
    0 讨论(0)
  • 2020-12-02 05:30
    print("<pre>".print_r($data,true)."</pre>");
    
    0 讨论(0)
  • 2020-12-02 05:31
    foreach($array as $v) echo $v, PHP_EOL;
    

    UPDATE: A more sophisticated solution would be:

     $test = [
        'key1' => 'val1',
        'key2' => 'val2',
        'key3' => [
            'subkey1' => 'subval1',
            'subkey2' => 'subval2',
            'subkey3' => [
                'subsubkey1' => 'subsubval1',
                'subsubkey2' => 'subsubval2',
            ],
        ],
    ];
    function printArray($arr, $pad = 0, $padStr = "\t") {
        $outerPad = $pad;
        $innerPad = $pad + 1;
        $out = '[' . PHP_EOL;
        foreach ($arr as $k => $v) {
            if (is_array($v)) {
                $out .= str_repeat($padStr, $innerPad) . $k . ' => ' . printArray($v, $innerPad) . PHP_EOL;
            } else {
                $out .= str_repeat($padStr, $innerPad) . $k . ' => ' . $v;
                $out .= PHP_EOL;
            }
        }
        $out .= str_repeat($padStr, $outerPad) . ']';
        return $out;
    }
    
    echo printArray($test);
    

    This prints out:

        [
            key1 => val1
            key2 => val2
            key3 => [
                subkey1 => subval1
                subkey2 => subval2
                subkey3 => [
                    subsubkey1 => subsubval1
                    subsubkey2 => subsubval2
                ]
            ]
        ]
    
    0 讨论(0)
  • 2020-12-02 05:31

    One-liner for a quick-and-easy JSON representation:

        echo json_encode($data, JSON_PRETTY_PRINT);
    

    If using composer for the project already, require symfony/yaml and:

        echo Yaml::dump($data);
    
    0 讨论(0)
提交回复
热议问题