Skip to content

Like print_r() in PHP but with an output that is readble for nested arrays

Notifications You must be signed in to change notification settings

bohnelang/print_lr-as-readble-and-linear-print_r-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

PHP print_lr()

This few lines of php code outputs a strong nested array / object-tree in a readable way:

print_r():
Array
(
    [items] => Array
        (
            [0] => Array
                (
                    [title] => Array
                        (
                            [0] => Array
                                (
                                    [language] => en
                                    [title] => Journal of Mass Spectrometry
                                    [language_phrases] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => en
                                                    [phrase] => English
                                                    [language] => en
                                                )

                                        )

                                )

                        )
print_lr():

[items][0][title][0][title]=Journal of Mass Spectrometry
[items][0][title][0][language_phrases][0][value]=en
[items][0][title][0][language_phrases][0][language]=en
[items][0][title][0][language_phrases][0][phrase]=English
[items][0][title][0][language]=en

This is a small recursiv function in php that walk through the nested structure and outputs the values:

function print_lr($arr,$display="") {
  foreach ($arr as $key => $value)  
    (is_array($value)||is_object($value))?print_lr($value, sprintf("%s[%s]",$display,$key)):printf("%s[%s]=%s\n",$display,$key,$value);
}

About

Like print_r() in PHP but with an output that is readble for nested arrays

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published