Reputation: 7279
I have an array:
$myArray = array('key1'=>'value1', 'key2'=>'value2');
I save it as a variable:
$fileContents = var_dump($myArray);
How can convert the variable back to use as a regular array?
echo $fileContents[0]; //output: value1
echo $fileContents[1]; //output: value2
Upvotes: 17
Views: 79618
Reputation: 3926
I wrote this function (out of fun:) and because I'm lazy AF I wanted a short way to convert an array inside a string to a valid PHP array. I'm not sure if this code is 100% safe to use in production as exec
and it's sisters always scare the crap out of me.
$myArray = 'array("key1"=>"value1", "key2"=>"value2")';
function str_array_to_php(string $str_array) {
// No new line characters and no single quotes are allowed
$valid_str = str_replace(['\n', '\''], ['', '"'], $str_array);
exec("php -r '
if (is_array($valid_str)) {
function stap(\$arr = $valid_str) {
foreach(\$arr as \$v) {
if(is_array(\$v)){
stap(\$v);
}
else {
echo \$v,PHP_EOL;
}
}
}
stap();
}'2>&1", $out);
return $out;
}
print_r(str_array_to_php($myArray));
Output:
Array ( [0] => value1 [1] => value2 )
As you can see, it will convert $myArray
into a valid PHP array, and then indexes it numerically and if it is multidimensional it will convert it into single one.
BE CAREFUL:
1- never pass the array in double quotes as this will allow null byte characters (\0) to be evaluated.
For example:
$myArray = "array(\"key1\"=>\"value\n\0 ggg\", \"key2\"=>\"value2\")"
//Output: Warning: exec(): NULL byte detected. Possible attack
2- This function wont work if
exec
is disabled (Mostly will be )3- This function wont work if
php
command is not set
One last thing, if you find any error or flaws please let me know in the comments so i can fix it and learn.
Hope this helps.
Upvotes: 4
Reputation: 2086
serialize might be the right answer - but I prefer using JSON - human editing of the data will be possible that way...
$myArray = array('key1'=>'value1', 'key2'=>'value2');
$serialized = json_encode($myArray);
$myNewArray = json_decode($serialized, true);
print_r($myNewArray); // Array ( [key1] => value1 [key2] => value2 )
Upvotes: 26
Reputation: 644
$array = ['10', "[1,2,3]", "[1,['4','5','6'],3]"];
function flat($array, &$return) {
if (is_array($array)) {
array_walk_recursive($array, function($a) use (&$return) { flat($a, $return); });
} else if (is_string($array) && stripos($array, '[') !== false) {
$array = explode(',', trim($array, "[]"));
flat($array, $return);
} else {
$return[] = $array;
}
}
$return = array();
flat($array, $return);
print_r($return);
OUTPUT
Array ( [0] => 10 [1] => 1 [2] => 2 [3] => 3 [4] => 1 [5] => '4' [6] => '5' [7] => '6'] [8] => 3 )
Upvotes: 4
Reputation: 351
How about eval? You should also use var_export with the return variable as true instead of var_dump.
$myArray = array('key1'=>'value1', 'key2'=>'value2');
$fileContents = var_export($myArray, true);
eval("\$fileContentsArr = $fileContents;");
echo $fileContentsArr['key1']; //output: value1
echo $fileContentsArr['key2']; //output: value2
Upvotes: 4
Reputation: 488384
I think you might want to look into serialize
and unserialize
.
$myArray = array('key1'=>'value1', 'key2'=>'value2');
$serialized = serialize($myArray);
$myNewArray = unserialize($serialized);
print_r($myNewArray); // Array ( [key1] => value1 [key2] => value2 )
Upvotes: 27
Reputation: 56106
Try using var_export to generate valid PHP syntax, write that to a file and then 'include' the file:
$myArray = array('key1'=>'value1', 'key2'=>'value2');
$fileContents = '<?php $myArray = '.var_export($myArray, true).'; ?>';
// ... after writing $fileContents to 'myFile.php'
include 'myFile.php';
echo $myArray['key1']; // Output: value1
Upvotes: 9