user982853
user982853

Reputation: 2488

Parse a string resembling PHP array syntax into an associative array

I have a key value pair string that I would like to convert to a functional array. So that I can reference the values using their key. Right now I have this:

$Array = "'Type'=>'Honda', 'Color'=>'Red'";
$MyArray = array($Array);

This is not bringing back a functional key/value array. My key value pairs are in a variable string which means the => is part of the string and i think this is where my problem is. Any help would be appreciated. All i am trying to do is convert the string to a functional key/value pair where I can grab the values using the key. My data is in a string so please don't reply with the answer "take them out of the string." I am aware that this will work:

$MyArray = array('Type'=>'Honda', 'Color'=>'Red');

But my probem is that the the data is already in the form of a string.

Upvotes: 0

Views: 240

Answers (4)

Jay Blanchard
Jay Blanchard

Reputation: 34426

Here is an example of one way you can do it -

$Array = "'Type'=>'Honda', 'Color'=>'Red'";
$realArray = explode(',',$Array); // get the items that will be in the new array
$newArray = array();
foreach($realArray as $value) {
    $arrayBit = explode('=>', $value); // split each item
    $key = str_replace('\'', '', $arrayBit[0]); // clean up
    $newValue = str_replace('\'', '', $arrayBit[1]); // clean up
    $newArray[$key] = $newValue; // place the new item in the new array
}
print_r($newArray); // just to see the new array
echo $newArray['Type']; // calls out one element

This could be placed into a function that could be extended so each item gets cleaned up properly (instead of the brute force method shown here), but demonstrates the basics.

Upvotes: 0

Whirlwind
Whirlwind

Reputation: 13675

You need to parse the string and extract the data:

$string = "'Type'=>'Honda', 'Color'=>'Red'";

$elements = explode(",",$string);

$keyValuePairs = array();
foreach($elements as $element){
$keyValuePairs[] = explode("=>",$element);
}

var_dump($keyValuePairs);

Now you can create your on array using the $keyValuePairs array.

Upvotes: 0

Joel Hinz
Joel Hinz

Reputation: 25414

You do need to "take them out of the string", as you say. But you don't have to do it manually. The other answer uses explode; that's a fine method. I'll show you another - what I think is the easiest way is to use preg_match_all() (documentation), like this:

$string = "'Type'=>'Honda', 'Color'=>'Red'";
$array = array();
preg_match_all("/'(.+?)'=>'(.+?)'/", $string, $matches);
foreach ($matches[1] as $i => $key) {
    $array[$key] = $matches[2][$i];
}
var_dump($array);

Upvotes: 1

Jason McCreary
Jason McCreary

Reputation: 73031

There is no direct way to do this. As such, you'll need to write a custom function to build the keys and values for each element.

An example specification for the custom function:

  • Use explode() to split each element based on the comma.
  • Iterate over the result and:
    • explode() on =>
    • Remove unnecessary characters, i.e. single quotes
    • Store the first element as the key and the second element as the value
  • Return the array.

Note: if your strings contain delimiters this will be more challenging.

Upvotes: 3

Related Questions