3

I have php script as below;

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';

$array = explode(",", $ages2);

echo $array["Peter"];

echo $ages["Peter"];

In this case, echo $ages["Peter"]; is working well, but echo $array["Peter"]; is not working. Can anybody solve this please..

Thanks in advance.

blasteralfred

3
  • Where are you getting that array as a string from? Commented Apr 17, 2011 at 13:31
  • 1
    var_dump($array); You have numeric-based array after explode(). Commented Apr 17, 2011 at 13:31
  • 1
    $ages2 threw me right off, it looked like an array (assumed copy/paste mistake). That's a bit weird. Commented Apr 17, 2011 at 13:43

5 Answers 5

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

0

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.

10 Comments

This will not work, you need to assign the value inside the eval'd expression.
If eval is the answer, you are probably asking the wrong question - Rasmus Lerdorf
@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: 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
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?"
|
0

You'll need to build the array yourself by extracting the name and age:

<?php
$array = array();
$ages2 = '"Peter"=>32, "Quagmire"=>30, "Joe"=>34';
foreach (explode(",", $ages2) as $element) {
    $parts = explode("=>", $element);

    if (count($parts) == 2) {
        $name = str_replace(array('"', ' '), '', $parts[0]);
        $age = (int) $parts[1];

        $array[$name] = $age;
    }   
}

print_r($array);

Comments

0

$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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.