json_encode之后的随机空值

时间:2017-01-01 21:20:50

标签: php json

当我在PHP中的数组上使用json_encode时,它后面会有随机空值。

function GiveUserServer($DBConnect, $Headers) {
  $OpenSessions = FindOpenSessions($UserID, $DBConnect, $Headers);
  if (JoinSession($OpenSessions, $DBConnect)) {
    print_r($OpenSessions);
    echo json_encode($OpenSessions);
  } else {
    echo "error";
  }}

function FindOpenSessions($UserID, $DBConnect, $Headers) {
  $OpenServers = QueryAllOpenServers($DBConnect);
  if (is_array($OpenServers)) {
    $ChosenServer = $OpenServers[rand(0, count($OpenServers))];
    if ($ChosenServer[0] == "" OR is_null($ChosenServer) OR $ChosenServer == "null") {
        SetupForServer($Headers, $DBConnect);
      }
    return $ChosenServer;
  } else {
    return "No Servers Found";
  }}

QueryAllOpenServers($ DBCONNECT);返回一个这样的数组:

Array ( [0] => Array ( [0] => 2 [1] => 0 [2] => 1 [3] => sadsa ) [1] => Array ( [0] => 3 [1] => 0 [2] => 65 [3] => sdasdfasf ) )

我是print_r()'以及FindOpenSessions返回的内容

Array ( [0] => 2 [1] => 0 [2] => 1 [3] => sadsa )

然而当我使用json编码时,它返回

["2","0","1","sadsa"]nullnullnullnullnullnullnull

空值的数量也各不相同。

1 个答案:

答案 0 :(得分:0)

通过添加array_map(utf8_encode,$ ChosenServer);

修复了该问题