Human readable arrays in PHP
2 min read

Human readable arrays in PHP

Human readable arrays in PHP

When I began using external APIs, I ran into a big problem. I couldn't read my arrays. They were a bumbling mess that looked like:

array(4) { ["time"]=> array(3) { ["updated"]=> string(25) "May 14, 2022 15:45:00 UTC" ["updatedISO"]=> string(25) "2022-05-14T15:45:00+00:00" ["updateduk"]=> string(25) "May 14, 2022 at 16:45 BST" } ["disclaimer"]=> string(155) "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org" ["chartName"]=> string(7) "Bitcoin" ["bpi"]=> array(3) { ["USD"]=> array(5) { ["code"]=> string(3) "USD" ["symbol"]=> string(5) "$" ["rate"]=> string(11) "28,763.1371" ["description"]=> string(20) "United States Dollar" ["rate_float"]=> float(28763.1371) } ["GBP"]=> array(5) { ["code"]=> string(3) "GBP" ["symbol"]=> string(7) "£" ["rate"]=> string(11) "23,451.3911" ["description"]=> string(22) "British Pound Sterling" ["rate_float"]=> float(23451.3911) } ["EUR"]=> array(5) { ["code"]=> string(3) "EUR" ["symbol"]=> string(6) "€" ["rate"]=> string(11) "27,625.2674" ["description"]=> string(4) "Euro" ["rate_float"]=> float(27625.2674) } } } Array ( [time] => Array ( [updated] => May 14, 2022 15:45:00 UTC [updatedISO] => 2022-05-14T15:45:00+00:00 [updateduk] => May 14, 2022 at 16:45 BST ) [disclaimer] => This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org [chartName] => Bitcoin [bpi] => Array ( [USD] => Array ( [code] => USD [symbol] => $ [rate] => 28,763.1371 [description] => United States Dollar [rate_float] => 28763.1371 ) [GBP] => Array ( [code] => GBP [symbol] => £ [rate] => 23,451.3911 [description] => British Pound Sterling [rate_float] => 23451.3911 ) [EUR] => Array ( [code] => EUR [symbol] => € [rate] => 27,625.2674 [description] => Euro [rate_float] => 27625.2674 ) ) )

We need it to look more like this:

Array
(
    [time] => Array
        (
            [updated] => May 14, 2022 15:54:00 UTC
            [updatedISO] => 2022-05-14T15:54:00+00:00
            [updateduk] => May 14, 2022 at 16:54 BST
        )

    [disclaimer] => This data was produced from the CoinDesk Bitcoin Price Index...
    [chartName] => Bitcoin
    [bpi] => Array
        (
            [USD] => Array
                (
                    [code] => USD
                    [symbol] => $
                    [rate] => 28,769.6520
                    [description] => United States Dollar
                    [rate_float] => 28769.652
                )
            ...
        )

)

YouTube Guide

The magic code

How to print an array

There are multiple ways to print an array. "print_r" is the official way, but var_dump will output the arr with more verbose information

However, both of these options are not human readable.

print_r($arr);
var_dump($arr);
echo '<pre>'.print_r($arr,true).'</pre>';

Making it human readable

If we want indenations, formatting, etc; we can use the following simple line of code:

echo '<pre>'.print_r($arr,true).'</pre>';

However, in projects, I don't want to type that mess. I put it inside of a function and call it every time I need an array printed. Since I use it so often, I have a very generic name for my function...

echo p($arr);

function p($arr){
    return '<pre>'.print_r($arr,true).'</pre>';
}