user400091
user400091

Reputation: 67

How to convert array string to array in php

I have a string, which is comes from js using encodeURICompoent(). Now i would like to convert this string into array. I tried my level best to solve this. But i couldn't find any solution to do this. Any help greatly appreciated.

array string = 'array(0=>array("packid"=>22,"pverid"=>18,"yaml"=>"- url: /static
 static_dir: static

- url: .*
  script: provider.py" ),1=>array("packid"=>23,"pverid"=>19,"yaml"=>"- url: /static
  static_dir: static

- url: .*
  script: provider.py" ));';

Thanking you, sureace

Upvotes: 0

Views: 1827

Answers (6)

MrWhite
MrWhite

Reputation: 45829

As mentioned by deceze, you can use eval() to turn this string into a PHP array, but whether you should or not is another matter...

$myCodeString = 'array(0=>ar ...... etc.';  // Your coded string

eval('$myArray = '.$myCodeString);
var_dump($myArray);

Which at least returns a valid PHP array:

array(2) {
  [0]=>
  array(3) {
    ["packid"]=>
    int(22)
    ["pverid"]=>
    int(18)
    ["yaml"]=>
    string(71) "- url: /static
 static_dir: static

- url: .*
  script: provider.py"
  }
  [1]=>
  array(3) {
    ["packid"]=>
    int(23)
    ["pverid"]=>
    int(19)
    ["yaml"]=>
    string(72) "- url: /static
  static_dir: static

- url: .*
  script: provider.py"
  }
}

You probably want to parse your "yaml" fields some more...?

Upvotes: 1

oezi
oezi

Reputation: 51797

it would be easier to use serialize or json for transferring an array. (but in that case, you would need a little function on clientside for serializing / json_encode)

EDIT: if encodeURIComponent is you problem, just do a urldecode before json_decode...

Upvotes: 0

user400091
user400091

Reputation: 67

yes i tried, but it dosen't solve my problem.

What i want is, I am getting json objects from client side to php, which contains encoded data string using javascript encodeURIComponent(). If it contains this data, i am unable to convert this object into array using json_decode() function.

json string=packs="{\"packid\":\"22\",\"pverid\":\"18\",\"yaml\":\"-%20url%3A%20%2Fstatic%0A%20%20static_dir%3A%20static%0A%0A-%20url%3A%20.%0A%20%20script%3A%20provider.py\"},{\"packid\":\"23\",\"pverid\":\"19\",\"yaml\":\"-%20url%3A%20%2Fstatic%0A%20%20static_dir%3A%20static%0A%0A-%20url%3A%20.%0A%20%20script%3A%20provider.py\"}"

Thanking you, sureace

Upvotes: -1

alexn
alexn

Reputation: 58952

It's very hard to understand what you are asking here.

Do you want to convert a yaml string to an php array? If so, try spyc.

Upvotes: 0

deceze
deceze

Reputation: 522005

It looks like eval should do the job of turning this string into an array.
BUT: You should never, ever use eval, especially with input that you get from external sources, since it opens up a slew of security issues. Instead, you should parse the string by hand, but that's going to be quite a nuisance.

You should find a better way to send the array values, for example via JSON. Especially when sending values from Javascript, this is the usual method.

Upvotes: 1

cypher
cypher

Reputation: 6992

Have a look at the implode function.

Upvotes: 0

Related Questions