Is it possible to such a line:

[['name' => 'id'], ['type' => 'INT(12)'], ['null' => true], ['prim' => true]], [['name' => 'test'], ['type' => 'VARCHAR(15)'], ['null' => false], ['prim' => true]] 

Convert to an ordinary array? If it is possible, then how to make it as simple as possible and desirable without parsing?

  • Without parsing - no way. - Dmitriy Simushev

3 answers 3

 $raw = '['. preg_replace(array("/\[([^[{])/","/([^}])\]/"),array("{ $1","$1 }"), str_replace(array("=>","'"),array(":",'"'),$input) ). ']'; $output = json_decode( $raw, true ); 

http://ideone.com/E69IOY

Not saving extra nesting

 $raw = str_replace(array("[[","]]","=>","'","[","]"),array("{","}",":",'"',"",""),$input); $output = json_decode('['.$raw.']',true); 

http://ideone.com/su8fOm

  • Strictly speaking, json also should not contain single quotes - Dmitriy Simushev
  • @ Alexey Shimansky did finish off, although ideone prevented me from doing this as best I could - rjhdby
  • @rjhdby, great, just super - perfect

Eval not very good, but as an option:

 $str = "[['name' => 'test'], ['type' => 'VARCHAR(15)'], ['null' => false], ['prim' => true]],[['name' => 'test'], ['type' => 'VARCHAR(15)'], ['null' => false], ['prim' => true]]"; $str = "[".$str."]"; $array = []; eval('$array='.$str.';'); print_r($array); 

http://sandbox.onlinephpfunctions.com/code/106dfcaf578a284cc1f63f27287d320835492d7f

  • and why Eval not good, it works just awesome? - perfect
  • Well, it depends on how you use it, of course, and it’s more difficult to maintain such a code; in general, it’s better not to get carried away. And about the danger you can read on the Internet. - Bookin
  • @perfect Replace the first line with "[['name' => 'test'], ['type' => 'VARCHAR (15)'], ['null' => false], ['prim' => true] ], [['name' => 'test'], ['type' => 'VARCHAR (15)'], ['null' => false], ['prim' => true]], phpinfo () "; and find out why :) - Firepro
  • Yes, @Firepro is right, the point is that if you are not sure about the data that will fall into eval, then process it until you wake up, or do not cram it into eval - Bookin
  • @Firepro, approximately I guess what will happen, the phpinfo disabled in the sandbox, there is no possibility to see it. - perfect

An example based on the response from rjhdby , but reducing the nesting of the array.

 $str = "[['name' => 'test'], ['type' => 'VARCHAR(15)'], ['null' => false], ['prim' => true]],[['name' => 'test'], ['type' => 'VARCHAR(15)'], ['null' => false], ['prim' => true]]"; $result = preg_replace("/\[(.*?)\]/", "$1", $str); $result = str_replace(["[","=>","'","]"],["{",":",'"',"}"],$result); $result = "[".$result."]"; var_dump(json_decode($result, true)); 

http://sandbox.onlinephpfunctions.com/code/fe62117607fcee8ad2e5c0a8961e70199b3441d6

  • Yes, it is more pleasant to the eye and the regular season is shorter, I added it, but let the primate for rjhdby remain the first discoverer, although I will use your option. - perfect
  • @perfect Well, then it can be even simpler, $raw = '['.str_replace(array("[[","]]","=>","'","[","]"),array("{","}",":",'"',"",""),$input).']'; ;; even without regulators;) Added to the answer - rjhdby
  • @rjhdby, wow, even better - perfect