Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse var_export output text to populate an associative array

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' => '[email protected]', '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?

like image 360
Sparsh Gupta Avatar asked Dec 12 '25 00:12

Sparsh Gupta


2 Answers

What about first exploding the string with the explode() function, using ', ' as a separator :

$str = "'middle_initial' => '', 'sid' => '1419843', 'fixed' => 'Y', 'cart_weight' => '0', 'key' => 'ABCD', 'state' => 'XX', 'last_name' => 'MNOP', 'email' => '[email protected]', 'city' => 'London', 'street_address' => 'Sample', 'first_name' => 'Sparsh',";
$items = explode(', ', $str);
var_dump($items);

Which would get you an array looking like this :

array
  0 => string ''middle_initial' => ''' (length=22)
  1 => string ''sid' => '1419843'' (length=18)
  2 => string ''fixed' => 'Y'' (length=14)
  3 => string ''cart_weight' => '0'' (length=20)
  ...


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 :

$result = array();
foreach ($items as $item) {
    if (preg_match("/'(.*?)' => '(.*?)'/", $item, $matches)) {
        $result[ $matches[1] ] = $matches[2];
    }
}
var_dump($result);

Which would get you :

array
  'middle_initial' => string '' (length=0)
  'sid' => string '1419843' (length=7)
  'fixed' => string 'Y' (length=1)
  'cart_weight' => string '0' (length=1)
  ...


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() or json_encode(), which can both be restored using unserialize() or json_decode(), respectively.

like image 50
Pascal MARTIN Avatar answered Dec 14 '25 14:12

Pascal MARTIN


Although I wholeheartedly agree with Pascal Martin, if you have this kind of data to deal with, the following (as Pascal's first suggestion mentions) could work depending on your actual content. However, do yourself a favor and store your data in a format that can be reliably put back into a PHP array (serialize, JSON, CSV, etc...).

<pre>
<?php

$str = "\" 'middle_initial' => '', 'sid' => '1419843', 'fixed' => 'Y', 'cart_weight' => '0', 'key' => 'ABCD', 'state' => 'XX', 'last_name' => 'MNOP', 'email' => '[email protected]', 'city' => 'London', 'street_address' => 'Sample', 'first_name' => 'Sparsh',\"";

function myStringToArray($str) {
    $str = substr($str, 1, strlen(substr($str, 0, strlen($str)-2)));
    $str = str_replace("'",'',$str);
    $strs = explode(',', $str);
    $arr = array();
    $c_strs = count($strs);
    for ($i = 0; $i < $c_strs; $i++) {
        if (strpos($strs[$i],'=>') !== false) {
            $_arr = explode('=>',$strs[$i]);
            $arr[trim($_arr[0])] = trim($_arr[1]);
        }
    }
    return $arr;
}

print_r(myStringToArray($str));

?>
</pre>

http://jfcoder.com/test/substr.php

Note, you would need to adjust the function if you have comma's within your array member's content (for instance, using Pascal's suggestion about the ', ' token).

like image 23
Jared Farrish Avatar answered Dec 14 '25 13:12

Jared Farrish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!