Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Why would this array:

Array
(
    [EventCode] => 20140709TXAO
    [ShortParticipant] => Array
        (
            [Address1] => null
            [Address2] => null
            [Address3] => null
            [City] => null
            [Country] => null
            [Email] => [email protected]
            [Employer] => TNA
            [FirstName] => Kim
            [LastName] => Kardashian
            [PID] => 1180133
            [Result] => null
            [State] => null
        )

)

be converted to the JSON below with json_encode? Note the null values are being converted to "null"!!! That is causing issues with my receiver.

{
    "EventCode": "20140709TXAO",
    "ShortParticipant": {
        "Address1": "null",
        "Address2": "null",
        "Address3": "null",
        "City": "null",
        "Country": "null",
        "Email": "[email protected]",
        "Employer": "TNA",
        "FirstName": "Kim",
        "LastName": "Kardashian",
        "PID": "1180133",
        "State": "null"
    }
}

Even the int value is being converted to a string, "1180133".

var_dump results:

array
  'EventCode' => string '20140709TXAO' (length=12)
  'ShortParticipant' => 
    array
      'Address1' => string 'null' (length=4)
      'Address2' => string 'null' (length=4)
      'Address3' => string 'null' (length=4)
      'City' => string 'null' (length=4)
      'Country' => string 'null' (length=4)
      'Email' => string '[email protected]' (length=27)
      'Employer' => string 'TNA' (length=3)
      'FirstName' => string 'Kim' (length=3)
      'LastName' => string 'Kardashian' (length=10)
      'PID' => string '1180133' (length=7)
      'Result' => string 'null' (length=4)
      'State' => string 'null' (length=4)

Javascript code:

function callRegStatus(eventcode, RegStatus) {
    inputdata = {LogonTicket: logonticket, 
                 data2: $.extend(true, {EventCode: eventcode}, 
                                       {ShortParticipant: RegStatus})};
    ok_to_proceed = false;
    $.ajax({async: false
          , type:'POST'
          , url: REGFUNCTIONS_URL
          , data: inputdata
          , dataType: 'json'
          , success: function(data) {          
                ok_to_proceed = true;
            }
          , error: function(jqXHR, textStatus, errorThrown) {
                ok_to_proceed = false;
                $("#error_message").html(jqXHR.responseText);
            }
    });
    return ok_to_proceed;
}

EE Plugin code:

 public function getRegStatus() {
    $data  = $_POST;
    $data2 = $data["data2"];

    $url = $this->server . '/RegStatus/'.$data["LogonTicket"];
    $options = array(CURLOPT_URL => $url,
                     CURLOPT_HEADER => false,
                     CURLOPT_SSL_VERIFYPEER => false,
                     CURLOPT_RETURNTRANSFER => true,
                     CURLOPT_POST => true,
                     CURLOPT_POSTFIELDS => json_encode($data2)
         );
    $ch = curl_init();
    curl_setopt_array($ch, $options);

    $RegStatusResult = curl_exec($ch);

    curl_close($ch); 
    return $RegStatusResult;
 }
share|improve this question
 
[email protected] :D –  OptimusCrime Dec 12 '13 at 21:45
 
You forgot to post the code –  Musa Dec 12 '13 at 21:45
 
This is not the default behavior of json_encode, as @Musa said, show us the code –  Rob M. Dec 12 '13 at 21:46
5  
And show the var_dump() of that array rather than the print_r() so we can see types and lengths. –  Michael Berkowski Dec 12 '13 at 21:47
1  
Those are just strings with the word null, not NULL values. –  AbraCadaver Dec 12 '13 at 22:04
show 2 more comments

2 Answers

up vote 1 down vote accepted

Before you encode your data with JSON you can do an recursive array walk like this:

array_walk_recursive($array, function(&$item, $key) {
    if ($item == 'null') $item = NULL;
});

Note the code uses lambdas and requires PHP 5.3.0 or higher. It can however easily be refactored if the anonymous function is defined before array_walk_recursive and then passed as callback — like so array_walk_recursive($array, 'nullStrToNull');.

As for the integers being casted as strings, the json_encode() option JSON_NUMERIC_CHECK (available since PHP 5.3.3) will encode numeric strings as numbers. Should the option not be available we can use array_walk_recursive() also.

share|improve this answer
add comment

Convert to UTF-8 first, e.g.

$array = array_map('utf8_encode', array('one', 'two', null, 'three'));
var_dump(json_encode($array));

You'll run into the same problem with true/false values.

share|improve this answer
 
nested array, won't work. –  MB34 Dec 12 '13 at 22:06
 
I'm not writing your code for you. Take the couple of minutes it would take to write a recursive function for it. You have a solution, now apply it yourself. –  Dave Dec 12 '13 at 22:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.