Jump to content

rescue my array!


mrjcfreak

Recommended Posts

A big shout for help!
I need to rescue an array from print_r format:
As a courtesy to myself, I got my script to make copies of an important array using the print_r function, so I have an array in human readable format... e.g.
[code]
    [1163980800] => Array
        (
            [date] => 1163980800
            [title] => Cathedral
            [img] => bloguploads/1163786440.jpg
            [blog] =>
            [cat] => Array
                (
                    [0] => Architecture
                )

            [id] => 1163980800
            [exif] => stuff
            [defaultbg] => 0
            [thumb] => Array
                (
                    [posx] => 74
                    [crop] => 184
                )

            [rating] => Array
                (
                    [0] => 1
                    [1] => 5
                )

            [count] => 88
            [comments] => Array
                (
                    [0] => Array
                        (
                            [date] => 1164631560
                            [name] => Sorrel
                            [message] => pohotoadn
                            [web] =>
                            [email] => *********
                        )

                )

        )

[/code]

This is obviously a snippet of a huge array...

How can I import this into php as a hash? (named element array?)

Some help would realy save my frazzled head right now!

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/29041-rescue-my-array/
Share on other sites

Hmm.. it's messy.  I think a recursive approach will work well.  I can't spare the time right now to do more then pseudocode.

[code=php:0]function rescue_array(&$lines, &$arr) {
  $line = array_shift($lines); # Fetches next line from the print_r output
  if (start of an array) {
    # Skip the '(' line
    rescue_array(&$lines, &$arr[$subarray]);
  } elseif (array entry) {
    $arr[$entry_name] = $entry_val;
  } elseif (line is ')' at end of array) {
    return;
  }
  # Process next element of array
  rescue_array(&$lines, &$arr);
}[/code]

Stack depth might be a problem if your array is REALLY huge..
Link to comment
https://forums.phpfreaks.com/topic/29041-rescue-my-array/#findComment-133122
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.