2

I have a string of the form:

string '["a"=>[10,20,30,40=>"Forty"],"b"=>[100,200,300,400=>"Four Hundred"],"c"=>[15]]' (length=78)

I need to convert this directly into an array as

array (size=3)
  'a' => 
    array (size=4)
      0 => int 10
      1 => int 20
      2 => int 30
      40 => string 'Forty' (length=5)
  'b' => 
    array (size=4)
      0 => int 100
      1 => int 200
      2 => int 300
      400 => string 'Four Hundred' (length=12)
  'c' => 
    array (size=1)
      0 => int 15

I have tried parse_str and json_encpde/decode and eval, but none of them see the variable as anything other than a string. Please help!

5
  • where did you get this anyway? Commented Mar 19, 2015 at 13:08
  • I am trying this approach. The string is actually retrieved as-is from a database, and I am wondering if this format would make it easier to process. Commented Mar 19, 2015 at 13:10
  • Can't test it right now but can you try eval-ing the thing? Another idea would be to write the string to php://memory and then do include 'php://memory' Commented Mar 19, 2015 at 14:35
  • 2
    Don't bother. Arrays aren't meant to be stored as literal strings in this way. Store it with json_encode or serialize then retrieve it with json_decode or unserialize Commented Mar 19, 2015 at 14:39
  • I know. :) Like I said, it was just an approach I wanted to try out. Commented Mar 19, 2015 at 15:31

2 Answers 2

1

One way to do it would be to just eval the thing

$string = '["a"=>[10,20,30,40=>"Forty"],"b"=>[100,200,300,400=>"Four Hundred"],"c"=>[15]]';
$array = eval("return $string");
print_r($array);

Keep in mind though that both of these solutions actually evaluate PHP code that you didn't write yourself. If you can, I'd suggest storing this data as JSON or using serialize_array() instead

Could not make this example to work, leaving it for archive purposes or for somebody else to give a try

Can't test it right now but one of these might be able to help you IF you run a PHP version that supports the [] array notation.

$string = '["a"=>[10,20,30,40=>"Forty"],"b"=>[100,200,300,400=>"Four Hundred"],"c"=>[15]]';
file_put_contents('php://memory', $string);
$array = include 'php://memory';
print_r($array);
Sign up to request clarification or add additional context in comments.

2 Comments

can you explain the code detailed, i can not able to understand this.i read the concept of eval function, its just convert the string into PHP code, after converting that , there is no result is display.
eval will simply evaluate aka run the given string. By design eval does not return a value, unless a return is executed in it. That's why in the second line of my example I add the return before the string variable
0
Try this hope will help this.    
$input_string =  '["a"=>[10,20,30=>"test",40=>"Forty"],"b"=>[100,200,300,400=>"Four Hundred"],"c"=>[15]]'."<br/>";
    $s1 = str_replace('],','-',str_replace(']]','',str_replace('[','',$input_string)));
    $s2 = explode("-",$s1);
    foreach ($s2 as $key => $value) {
        $s3[] = explode("=>",str_replace('=>"','-"',$value));
        $s4[$s3[$key][0]] = $s3[$key][1];

    }
    foreach($s4 as  $keys => $v) {
        $value = explode(",",$v);
        foreach($value as $val) {
            if(strrchr($val,'-'))
            {
                $final[$keys][array_shift(explode("-",$val))] = end(explode("-",$val));
            }
            else
                $final[$keys][] = $val;
        }
    }

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.