1

I'm sending something like this to PHP using AJAX:

var details = [{name: "thing1", value: true}, {name: "thing2", value: true}, {name: "thing3", value: false}]

$.ajax({
    url: 'myphp.php',
    type: 'POST',
    dataType: 'JSON',
    data: {
        universalDetails: details,
    },
//...etc.

In my PHP, if I include this:

$universalDetails = $_POST["universalDetails"];
var_dump($universalDetails);

All of my values are shown as strings in an array...like this:

{ ["name"]=> string(20) "thing1" ["value"]=> string(4) "true" }

What's happening here? How do I preserve these as booleans?

jonmrich
  • 4,233
  • 5
  • 42
  • 94

1 Answers1

1

That's by design, because each POST/GET Parameter is basically a string. You could try to do if($_POST['universalDetails']['value'] == 'true') ... because PHP will change a string "true" to a boolean with the value of true(not a string!):

More information: https://secure.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

But notice: var_dump((bool) "false"); // bool(true)

jens1o
  • 625
  • 7
  • 14