6

Possible Duplicate:
Create array printed with print_r

Duplicate of How create an array from the output of an array printed with print_r? which also has a nice code example to solve this

I need to reverse an error log which has as output print_r($data,true).
Example data would look like:

Array
(
    [subject] => this is the subject
    [body] => <p>Dear user,</p><p>this is the body of the email</p>
    [from_id] => 0
    [from_email] => admin@karakas.org
    [to] => Array
        (
            [0] => Array
                (
                    [id] => 0
                    [email] => 64909
                )

        )

    [send_to_inbox] => 1
)
Community
  • 1
  • 1
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • 3
    While not a solution - If you control the `error_log` call, in the future you could `error_log(serialize($data));` Which can be unserialized – cloakedninjas Jan 08 '13 at 15:45
  • You could use a regex to do it – ethrbunny Jan 08 '13 at 15:47
  • Might look into using `var_export` instead of `print_r` in the future if you keep running into this. I'm not certain you can simply "reverse" `print_r` because string starts/ends are ambiguous. – Mr. Llama Jan 08 '13 at 15:49
  • When you say "reverse", what format would you like to end up with? And will this code (that does the reversing) be deployed to production, or just some scaffold to help you troubleshoot a problem? (Some strategies may or may not be valid depending on what you're trying to accomplish.) – Chris Allen Lane Jan 08 '13 at 15:55
  • 1
    Use @hakre's script here https://gist.github.com/1102761 I use it and it works great. – kittycat Jan 08 '13 at 15:58
  • the question is not whether you can do this, but why you'd want to do it. `print_r()` is intended only as output for a developer to read to help with debugging; there's no reason to parse it in that context, and you shouldn't be using it for anything else; if you're doing anything else, there are better output formats you can use. – SDC Jan 08 '13 at 16:01
  • @SDC welcome to real life where u do not control everything in the world. This is the task I have, how it came to be...we can sit by the fire and tell stories about it. – Itay Moav -Malimovka Jan 08 '13 at 16:30
  • If you absolutely have to parse `print_r()` output, then @Jails's `print_r_reverse()` solution or similar is likely the only usable answer open to you. But you need to be aware that the format `print_r` generates is NOT GUARANTEED TO BE PARSEABLE. It's just not designed for it. You may well find examples that break your parser. Work with what you've got now if you need to, but I strongly recommend changing the log format as soon as possible -- oh, and go find the developer who originally thought that print_r() was a good format for a log, and hit them with a wet fish. ;-) – SDC Jan 08 '13 at 16:40

2 Answers2

12

In the PHP manual there's a print_r_reverse() function in comments : http://php.net/manual/en/function.print-r.php

However var_export() can be an alternative if your logs are generated using var_export(). This way, you only need eval() to retrieve the exported array.

SDC
  • 14,192
  • 2
  • 35
  • 48
Jails
  • 370
  • 1
  • 4
  • 9
  • print_r_reverse() from the comments works awesome, thank you! I know it shouldn't be done this way but i really needed to parse that ****. – Dominik Mayrhofer May 03 '22 at 06:26
5

The output of print_r() is not designed to parsed; it's designed to be read by a developer for debugging purposes. You should not be trying to parse it.

If you really must parse a PHP data dump of this nature, the var_export() function is intended for this kind of thing. However, I wouldn't recommend parsing this either -- it's still unlikely to be the best solution for you.

If your intention is to store a string representation of an array structure, and then parse it later, you would be better off using either the serialize()/unserialize() functions, or the json_encode()/json_decode() functions.

Both of these will give you a much more reliable and easily parseable data dump format. Of the two, I'd recommend json_encode() every time, as not only is it easy to work with, it's also supported by other languages, easy to read manually, and compact.

In short, don't parse print_r() output; use json_encode()/json_decode() instead.

http://uk1.php.net/manual/en/function.json-encode.php

SDC
  • 14,192
  • 2
  • 35
  • 48