Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert PHP array string into an array

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
like image 820
Peter Craig Avatar asked Mar 26 '09 04:03

Peter Craig


People also ask

How do I convert a string to an array in PHP?

The str_split() is an inbuilt function in PHP and is used to convert the given string into an array. This function basically splits the given string into smaller strings of length specified by the user and stores them in an array and returns the array.

How convert string to array in PHP with explode?

Explode MethodPass a delimiter and a string to the explode function, and it splits the string into array elements, where it finds the delimiter. The delimiter can be a single character or it can be multiple characters.

How do you implode an array?

The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function. If we have an array of elements, we can use the implode() function to join them all to form one string.


1 Answers

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 ) 
like image 184
Paolo Bergantino Avatar answered Sep 22 '22 03:09

Paolo Bergantino