1

I am looking for a way to parse strings in an array to an array which has a similar pattern to how CakePHP handles POST data. Or even a function in CakePHP that would do it.

UPDATED:

Current array:

array(
    'data[callers]' => (int) 4,
    'data[status]' => 'Unknown',
    'data[country_id][107]' => (int) 1,
    'data[country_id][150]' => (int) 0
)

Desired result:

array(
    'callers' => (int) 4,
    'status' => 'Unknown',
    'country_id' => array(
        (int) 107 => (int) 1,
        (int) 150 => (int) 0
    )
)

The purpose is saving serialized form data which can later be passed to a PHP function without having to POST the data from the browser.

The data comes from a form which was serialized and saved in the database. CakePHP generates input names in the form with brackets like this: data[country_id][107] and inside the controller you can access it like this $this->request->data['country_id']['107'] But when I serialize the form with javascript and save the raw JSON string in the database I need a way to make it into an array like CakePHP does.

Morten Twellmann
  • 125
  • 2
  • 13
  • 1
    It would be better if you'd post valid PHP syntax, ie an actual dump as produced by `debug()`. Looking at the current array it's hard to tell whether you're just being creative, or whether the left hand side values are supposed to resemble simple string keys. Knowing about the context would also help, ie where does the original array stem from? – ndm Apr 19 '17 at 21:27
  • @ndm I updated the quest to make it more clear what I'm trying to solve. – Morten Twellmann Apr 21 '17 at 20:22
  • Can i ask why you are saving it as raw json string in your DB and not formatting it nicely into fields before saving? it looks like you are making things harder for yourself. – Jason Joslin Apr 22 '17 at 05:38
  • @JasonJoslin I have a form with a lot of different fields that are basically settings for the application. The fields are used to generate an SQL query by submitting the form data to the server. The user can save his settings and later load the same settings into the form. I save the settings by jsonify and load them back into the form with dejsonify via AJAX. But now, as I am writing this I realized I'm probably making it unnecessarily complicated. I could just json_encode the array and save it in the DB as well... – Morten Twellmann Apr 22 '17 at 09:39

1 Answers1

1

Firstly make sure your array is valid first like:

$data = array (
    'callers' => 4,
    'status' => 'Unknown',
    'country_id' => array(
        '107' => 0,
        '150' => 0
    )
);

JSON ENCODE

Now you can json encode it

 $json = json_encode($data);
 echo $json; // prints: {"callers":4,"status":"Unknown","country_id":{"107":0,"150":0}}

See ^ it is now a string.

http://php.net/manual/en/function.json-encode.php

JSON DECODE

Then when you need it as an array call json_decode()

json_decode($data, true);

Note the second parameter is setting return array to true else you will get an the json returned as an object.

http://php.net/manual/en/function.json-decode.php

Jason Joslin
  • 1,154
  • 8
  • 23