mrjcfreak Posted November 30, 2006 Share Posted November 30, 2006 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! Quote Link to comment Share on other sites More sharing options...
btherl Posted December 1, 2006 Share Posted December 1, 2006 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.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.