0

I'm trying to set user permissions, they are stored as an array in the database.. This is the current output from my html form...

{
"where_to_buy": "true",
"spec_sheet": "true",
"dwg_access": "true",
"bim_access": "true",
"product_prices": "true",
"portal_access": "true",
"save_quote": "true",
"request_drawings": "true",
"place_order": "true",
"special_offer": "true"
}

However I need it to be formatted like so:

{
"where_to_buy": true,
"spec_sheet": true,
"dwg_access": true,
"bim_access": true,
"product_prices": true,
"portal_access": true,
"save_quote": true,
"request_drawings": true,
"place_order": true,
"special_offer": true
}

How do I stop or convert my html form submission from strings to just true or false values?

Sterlingman
  • 61
  • 1
  • 5
  • 2
    Possible duplicate of [Bool parameter from jQuery Ajax received as literal string "false"/"true" in PHP](http://stackoverflow.com/questions/7408976/bool-parameter-from-jquery-ajax-received-as-literal-string-false-true-in-php) – swidmann Nov 04 '15 at 09:42
  • 3
    I somehow miss the connection between the question title and text.... – VolkerK Nov 04 '15 at 09:42
  • You already store those strings as strings in the database? Please elaborate. – VolkerK Nov 04 '15 at 09:48
  • I kind of mixed up the question title with something else, sorry! I'll change it if I can. – Sterlingman Nov 04 '15 at 09:55

2 Answers2

4

You can simply use json_decode along with array_map like as

echo json_encode(array_map(function($v) {
        if ($v == "true") {
            return true;
        } else {
            return false;
        }
    }, json_decode($json, true)));
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
1

You can map values with array_walk

This also allows you to add more than true/false if required later

// from json   
$yourArray = json_decode($yourJSON, true);

// map values
array_walk($yourArray, function(&$v) {
    $map = array(
        'true' => true,
        'false' => false
        // more as needed
    );

    if ( isset($map[$v]) ) {
        $v = $map[$v];
    }
});

// back to json
$output = json_encode($yourArray);

Edit: both at once, see comments

$yourArray = json_decode($yourJSON, true);
$yourFormattedArray = array();
$map = array(
    'true' => true,
    'false' => false
    // more as needed
);

foreach ($yourArray as $key => $val ) {
    if ( isset($map[$val]) ) {
        $val = $map[$val];
    }
    $yourFormattedArray[str_replace('_', '.', $key)] = $val;
}

$yourFinalArray = json_encode($yourFormattedArray);
danjam
  • 1,064
  • 9
  • 13
  • Facepalm. Totally missed that, title threw me - thanks. Added encode/decode – danjam Nov 04 '15 at 09:57
  • Your original code worked fine for me.. The new problem is that I need to have "."'s in the array keys instead of "_".... Apparently you can't name your form inputs with .'s it willl just convert them to _'s anyway. I didn't notice at first that they weren't "."'s. – Sterlingman Nov 04 '15 at 10:26
  • That's really a seperate question, but I've added it for you – danjam Nov 04 '15 at 10:39