使用扁平数组

Because of an external service, I am forced to store all my data using a flattened JSON Object.

Normally, I would retrieve my JSON Object, turn it into a PHP array, and use it like so:

Decode this JSON Object into an array named $array

{
  "info":{
    "title:":"This is a test title",
    "owner":"1"
  },
  "data":{
    "page_1":{
      "first_name":null,
      "address":{
        "address_line1":null,
        "address_line2":null
      }
    }
  }
}

Then do something like this:

$page_1 = $array['data']['page_1'];
echo $page_1['first_name'];
echo $page_1['address']['address_line1'];

Instead I have to do something like this:

{
  "info.title:": "This is a test title",
  "info.owner": "1",
  "data.page_1.first_name": null,
  "data.page_1.address.address_line1": null,
  "data.page_1.address.address_line2": null
}

So I guess my question is, what is the best way to work with a flattened array in PHP?

  • Should I un-flatten it on the spot?
  • Should I just use it like echo $array['data.page_1.first_name'];?
  • Something that I'm not thinking of

What do you think the best practice would be?