-1

I have created total api's using php programming, for the output i used

json_encode($arr)

Now i want to print output by replacing null with "".

I have already completed 400+ webservices using json_encode($arr) to output, but i don't want to change to

can any body help

My Actual Array

$array=array("id" => "2",
"name" => "Test Name",
"address" => "83 Test Street",
"city" => "Test",
"phone" => "(123) 456-7890",
"video_thumb" => null,
"thumbnail" => null,
"description" => null);

echo json_encode($array); //output:

{"id":"2",
"name":"Test Name",
"address":"83 Test Street","city":"Test","phone":"(123) 456-7890","video_thumb":null,
"thumbnail":null,
"description":null
}

Expecting output //output

    {"id":"2","name":"Test Name",
"address":"83 Test Street",
"city":"Test","phone":"(123) 456-7890","video_thumb":"",
"thumbnail":"",
"description":""}
Manikanta
  • 193
  • 3
  • 17
  • 1
    It’s never a good idea to override existing functions... just refactor your code and make that effort now before you get yourself in deeper problems ;-) – David Heremans Dec 19 '18 at 06:06
  • please explain more – TarangP Dec 19 '18 at 06:09
  • 4
    overriding a system function is, based on the system in use, either very bad, bad, baaad practice or very not possible. the best way to go would be fixing the consuming code so it can correctly handle `null`, which is a valid JSON value. the second best way is to write your own wrapper function `json_encode_no_null` and replace all calls to json_encode. but don't be surprised when you accidentally break something that _needs_ `null` instead of `""`. if you still insist on overriding a function: https://stackoverflow.com/a/3620748/5309228 - but you won't be happy with it. – Franz Gleichmann Dec 19 '18 at 06:09
  • that's not how you should solve your problem OP. Adjust the codes that uses the API instead. – ACD Dec 19 '18 at 06:19

4 Answers4

0

Like this

$array = array("id" => "2",
    "name" => "Test Name",
    "address" => "83 Test Street",
    "city" => "Test",
    "phone" => "(123) 456-7890",
    "video_thumb" => null,
    "thumbnail" => null,
    "description" => null
);

array_walk_recursive($array, function(&$item){
    if(is_null($item)) $item = '';
});

echo json_encode($array);

Output

{"id":"2","name":"Test Name","address":"83 Test Street","city":"Test","phone":"(123) 456-7890","video_thumb":"","thumbnail":"","description":""}

Sandbox

If you have to call it 400 times, just wrap it in a function. Decorate the normal json_encode like this:

function my_json_encode($value,$options=0,$depth=512){
     array_walk_recursive($value, function(&$item){
        if(is_null($item)) $item = '';
     });

     return json_encode($value,$options,$depth);
}

This way you can access all the options and functionality of the normal method, but with one small change. There is no "overriding" of core functions, no in the way you can override a class method. Functions can be defined only one time, then they are set.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • Same question for you too: are you sure that you understand the task? OP has ~400 places in the code with `json_encode`. Do you really suggest him edit it 400 times with the sample above? – user1597430 Dec 19 '18 at 06:29
  • It's trivial to wrap it in a function and call it 400 times, he calls json_encode 400 times, no? Whats the difference between calling my_json_encode and that. – ArtisticPhoenix Dec 19 '18 at 06:31
  • OP didn't ask how to replace `null` with `""` - I guess he can do it manually without your help. His question is "How to override json_encode() functionality". Read the title. – user1597430 Dec 19 '18 at 06:33
  • You cannot "override" a core PHP function, as you would override a class method. You can only replace it for something else. – ArtisticPhoenix Dec 19 '18 at 06:35
  • You CAN override a core PHP function, check Franz Gleichmann's answer first or visit directly http://php.net/manual/en/function.override-function.php. – user1597430 Dec 19 '18 at 06:36
  • `overriding a system function is, based on the system in use, either very bad, bad, baaad practice or very not possible. the best way to go would be fixing the consuming code so it can correctly handle null, which is a valid JSON value. the second best way is to write your own wrapper function json_encode_no_null and replace all calls to json_encode` I read it, how bout you read my answer. – ArtisticPhoenix Dec 19 '18 at 06:38
  • And PS that's not an answer, it's a comment. According to that "answer" what I suggest above is the second best way, the first makes to many assumptions to warrant an answer. IE. I have no way to know if the OP can `he best way to go would be fixing the consuming code` Therefore the only reasonable answer, is one in the line of what I provided :-)~ – ArtisticPhoenix Dec 19 '18 at 06:39
  • Also the issue with overriding the default functionality, is obviously, what if you actually want/need NULL in some other piece of JSON? What do you do then. – ArtisticPhoenix Dec 19 '18 at 06:46
0

This code helped me to fix the problem without modifying all the services

ob_start(function($json) {
    function json_validator($data = NULL) {
        if (!empty($data)) {
            json_decode($data);
            return (json_last_error() === JSON_ERROR_NONE);
        }

        return false;
    }

    if (json_validator($json)) {
        $response_arry = json_decode(str_replace('null', '""', $json));
        if (json_last_error_msg() == "Syntax error") {
            $response_arry = json_decode(str_replace('"null"', '""', $arr));
        }
        return json_encode($response_arry, JSON_PRETTY_PRINT);
    } else {
        return ob_get_contents();
    }
});
Manikanta
  • 193
  • 3
  • 17
  • _Try this_ answer are low-value here. Always add an explanation of how your solution works and why it is a good idea. – mickmackusa Dec 19 '18 at 09:22
0

The idea is to use override_function to "modify" the json_encode function. Before calling this function it is possible to modify the array so that it satisfies an empty string rather than null. You can try this:

rename_function('json_encode', 'override_json_encode' );
override_function("json_encode", '$array', 'return override_json_encode($array);' );


function override_json_encode($array){
    foreach ($array as &$value) {
        if ($value === null) 
             $value = "";
    }
return json_encode($array);
}

}

simona
  • 15
  • 4
  • 1
    _Try this_ answer are low-value here. Always add an explanation of how your solution works and why it is a good idea. – mickmackusa Dec 19 '18 at 09:22
  • The idea is to use override_function to "modify" the json_encode function. Before calling this function it is possible to modify the array so that it satisfies an empty string rather than null. What do you think about it? – simona Dec 19 '18 at 09:41
-1

oh, understand your mean. please view follow document: http://php.net/manual/en/ref.apd.php rename_function and override_function

Nick Wang
  • 624
  • 3
  • 6
  • Oh sweet my -- NO. Let's agree to not advise developers to needlessly hack at json strings with `preg_replace()`. Find another way. – mickmackusa Dec 19 '18 at 06:45