32 And, then, for each value, explode usi..." />

Php exploded associative array problem?

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.

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.

Of course it doesn't work. Explode just splits by the given delimiter but doesn't create an associative array.

Your only hope if you really have such a string is to parse it manually. Either using preg_match_all, or I suppose you could do: $array = eval('return array('. $ages2.');'); But of course this isn't recommended since it could go wrong in many many ways.

In any case I'm pretty sure you can refactor this code or give us more context if you need more help.

This will not work, you need to assign the value inside the eval'd expression. – zerkms Apr 17 at 13:35 2 If eval is the answer, you are probably asking the wrong question - Rasmus Lerdorf – Madmartigan Apr 17 at 13:36 @zerkms, right, I so never use eval that I forgot it has to return, fixed. @Madmartigan: indeed this is what I'm trying to say.

I'm not pretending this is a good solution :) – Seldaek Apr 17 at 13:38 @Seldaek: oh, did not know that return in eval acts like you wrote :-S Though I never used it in my 8 years php practice :-S – zerkms Apr 17 at 13:40 That is some creative thinking though! I think the fact that this guy is using a string that looks like an assoc array is kind of muddling. This definitely feels like one of those questions where you have to ask: "WHY are you doing this?" – Madmartigan Apr 17 at 13:45.

You'll need to build the array yourself by extracting the name and age: 32, "Quagmire"=>30, "Joe"=>34'; foreach (explode(",", $ages2) as $element) { $parts = explode("=>", $element); if (count($parts) == 2) { $name = str_replace(array('"', ' '), '', $parts0); $age = (int) $parts1; $array$name = $age; } } print_r($array).

$ages2 is not an array, so what you're trying here won't work directly, but you can transform a string with that structure into an array like this: $ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34'; $items = explode(",", $ages2); foreach ($items as $item) { list($key,$value) = explode('=>',$item); $key = str_replace('"','',trim($key)); // Remove quotes and trim whitespace. $array$key = (int)$value; } If you var_dump($array), you'll have: array(3) { "Peter"=> int(32) "Quagmire"=> int(30) "Joe"=> int(34) } So you can do this as expected and get 32 back out: echo $array'Peter'.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions