Question

I have never really thought about this until today, but after searching the web I didn't really find anything. Maybe I wasn't wording it right in the search.

Given a nested array where var_dumped

<pre>array(3) { 'id' =>  string(19) "as-agent-to-shipper" 'name' => string(19) "as-agent-to-shipper" 'children' => array(1) {
[0] =>
array(3) {
  'id' =>
  string(28) "agility-logistics-s-a-de-c-v"
  'name' =>
  string(30) "AGILITY LOGISTICS S.A. DE C.V."
  'data' =>
  array(2) {
    'band' =>
    string(22) "-AS AGENT TO SHIPPER -"
    'relation' =>
    string(13) "Buyer of band"
      }
    }
  }
}

The challenge is this: What is the best optimized method for converting a var_dumped array to json? Is it just a matter of regex? Or is there some other way? I am looking for creativity.

Was it helpful?

Solution

It most likely is impossible to parse this structure using RegEx.

It would be possible to write a specialized parser for it, but given that PHP makes no promises on its format, it is a very bad idea to do so. In a minor PHP version update the format could change and the parser would stop working.

Even more, var_dump is lossy, that is there are cases when you can not recover the state of an object just by its representation. The simplest example would be resources. var_dumping an open file returns resource(5) of type (stream), which is completely not enough to do anything useful with it.

All in all, it just sounds like a bad idea, and whatever problem would be solved by it, should be solved differently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top