You'll have to go in two steps :
- First, explode using
', ', as a separator ; to get pieces of data such as "Peter"=>32
- And, then, for each value, explode using
'=>' as a separator, to split the name and the age
- Removing the double-quotes arround the name, of course.
For example, you could use something like this :
$result = array();
$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';
foreach (explode(', ', $ages2) as $couple) {
list ($name, $age) = explode('=>', $couple);
$name = trim($name, '"');
$result[$name] = $age;
}
var_dump($result);
And, dumping the array, you'd get the following output :
array
'Peter' => string '32' (length=2)
'Quagmire' => string '30' (length=2)
'Joe' => string '34' (length=2)
Which means that using this :
echo $result['Peter'];
Would get you :
32
var_dump($array);You have numeric-based array afterexplode().$ages2threw me right off, it looked like an array (assumed copy/paste mistake). That's a bit weird.