Sometime back I was getting alot of data via some API and I saved it into a flat file doing a simple var_dump or print_r. Now I am looking to process the data and each line looks like:
” ‘middle_initial’ => ”, ‘sid’ => ‘1419843’, ‘fixed’ => ‘Y’,
‘cart_weight’ => ‘0’, ‘key’ => ‘ABCD’, ‘state’ => ‘XX’, ‘last_name’
=> ‘MNOP’, ’email’ => ‘abc@example.com’, ‘city’ => ‘London’,
‘street_address’ => ‘Sample’, ‘first_name’ => ‘Sparsh’,”
Now I need to get this data back into an array format. Is there a way I can do that?
What about first exploding the string with the
explode()function, using', 'as a separator :Which would get you an array looking like this :
And, then, iterate over that list, matching for each item each side of the
=>, and using the first side of=>as the key of your resulting data, and the second as the value :Which would get you :
But, seriously, you should not store data in such an awful format :
print_r()is made to display data, for debugging purposes — not to store it an re-load it later !If you want to store data to a text file, use
serialize()orjson_encode(), which can both be restored usingunserialize()orjson_decode(), respectively.