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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.