为什么 PHP json_encode 不会从传入的数组中返回正确的值?

Why would PHP json_encode not returning the correct value from passed in array?

全部。我正在开发内置于 PHP 中的自定义构建 API。我有一个从我的数据库访问对象返回的数组(见下文)。当我使用 json_encode 函数时,分配给 phone1Type 的 int 值编码不正确。我已经对多个记录进行了尝试,编码的 json 对象中的值始终与 phone2Type 匹配。关于可能发生的事情的任何想法? (除了相应的 json 对象外,我还在下面包含了两个示例数组。)

我用来检查数组和 json 值的代码如下:

$responseObject = $userCtrl->selectPersonForUserId($userId);
var_dump($responseObject);
var_dump(json_encode($responseObject));

一个要编码的数组示例如下。 (phone1Type 和 phone2Type 键位于最后,但为了完整起见,此处包括完整数组。另外,作为旁注,数组中的其他 int 值编码良好。)

object(Adult)#13 (8) {
  ["person":protected]=>
  object(Person)#14 (4) {
    ["id":protected]=>
    int(3)
    ["firstName":protected]=>
    string(7) "William"
    ["lastName":protected]=>
    string(3) "Smith"
    ["hasVerified":protected]=>
    bool(false)
  }
  ["address":protected]=>
  object(Address)#17 (4) {
    ["id":protected]=>
    int(2)
    ["address1":protected]=>
    string(15) "520 Hilbert Dr."
    ["address2":protected]=>
    string(0) ""
    ["city":protected]=>
    object(City)#18 (3) {
      ["zip":protected]=>
      string(5) "71342"
      ["city":protected]=>
      string(11) "West Monroe"
      ["state":protected]=>
      string(2) "AL"
    }
  }
  ["email":protected]=>
  string(14) "wmrmay@spam.com"
  ["phone1":protected]=>
  string(10) "6195080000"
  ["phone1Type":protected]=>
  int(1)
  ["phone2":protected]=>
  string(10) "3188126574"
  ["phone2Type":protected]=>
  int(0)
  ["teacher":protected]=>
  NULL
}

这将编码为以下 json 对象:

{"person":{"id":3,"firstName":"William","lastName":"Smith","hasVerified":false},"address":{"id":2,"address1":"520 Hilbert Dr.","address2":"","city":{"zip":"71342","city":"West Monroe","state":"AL"}},"email":"wmrmay@spam.com","phone1":"6195080000","phone1Type":0,"phone2":"3188126574","phone2Type":0,"teacher":null}

为简洁起见,这是另一个数组的最后几行,后面是 json 对应的数组:

  ["email":protected]=>
  string(20) "wltrallen2@gmail.com"
  ["phone1":protected]=>
  string(10) "6192047586"
  ["phone1Type":protected]=>
  int(1)
  ["phone2":protected]=>
  NULL
  ["phone2Type":protected]=>
  NULL
  ["teacher":protected]=>
  NULL
"email":"wltrallen2@gmail.com","phone1":"6192047586","phone1Type":null,"phone2":null,"phone2Type":null,"teacher":null}

编辑以添加原始 Adult.php 模型 class:

class Adult implements JsonSerializable {
    protected $person; // Person object
    protected $address; // Address object
    protected $email;
    protected $phone1;
    protected $phone1Type; // PhoneType object
    protected $phone2;
    protected $phone2Type; // PhoneType object
    protected $teacher; // Teacher object

    public function __construct($person, $address, $email, $phone1, $phone1Type, $phone2, $phone2Type, $teacher)
    {
        $this->person = $person;
        $this->address = $address;
        $this->email = $email;
        $this->phone1 = $phone1;
        $this->phone1Type = $phone1Type;
        $this->phone2 = $phone2;
        $this->phone2Type = $phone2Type;
        $this->teacher = $teacher;
    }

... // Getters and Setters removed for brevity

    private function getPhoneType($type) {
     if(PhoneTypes::isValid($type)) {
          return PhoneTypes::StringDict[$type];
        }

        return '';
    }
    
    function jsonSerialize() {
     $array = [
       'person' => $this->person,
       'address' => $this->address,
       'email' => $this->email,
       'phone1' => $this->phone1,
       'phone1Type' => $this->phone2Type,
       'phone2' => $this->phone2,
       'phone2Type' => $this->phone2Type,
       'teacher' => $this->teacher
     ];
 
     return $array;
   } 
}

所以,令人尴尬的是,这只是最初的错字,然后是我的健忘。

  1. 在 Adult.php 模型 class 中,我实现了 JsonSerializable 接口以确保 Adult 对象可以在 Json 中编码。这样做时,我在构建数组时犯了一个印刷错误:
       'phone1Type' => $this->phone2Type,

当然应该是...

       'phone1Type' => $this->phone1Type,

这个错字是我问题的根源。呃!

  1. 然而,由于我已经深深地陷入了这个项目,并且自从我最初构建这些模型 classes 以来已经有一段时间了,我完全忘记了一个对象可以被 JSON 它必须实现 JsonSerializable 接口。因此,在调试时,我从来没有想过要在文件的最后回顾我的模型来检查 jsonSerialize() 函数。 (在这里插入面部手掌。)

感谢您的回复。这是我第一次真正 post 关于 Whosebug 的问题,感谢大家的关注。抱歉,这不是一个令人兴奋的问题,而只是一个愚蠢的新手程序员时刻。