JSON数组对象值返回null

时间:2018-06-28 17:46:08

标签: php json

我不知道我似乎做错了什么,当我尝试显示带有JSON的数组时,该值在所有对象上都返回null,但是当我print_r数组时,它会完美返回数组,但是当我尝试用php显示该代码,我的代码返回null值。

//api.php
$cust = new Customersss;
$customers = $cust->getDeliveryDetail();    

运行查询并打印出 $ customers

 //still api.php       


          $stmt = $this->dbConn->prepare("SELECT payment_method, date_of_delivery, set_date_of_delivery, waybill_number, shipment_type, country_from, state_from, area_from, country_to, state_to, area_to, street_address_to, name_of_shipment_owner, email_of_shipment_owner, phone_of_shipment_owner, phone_of_shipment_owner_1 FROM `all_shipments` WHERE waybill_number = :waybill_number AND password_for_shipments = :password_for_shipments");
          $stmt->bindParam(':waybill_number', $this->waybill_number);
          $stmt->bindParam(':password_for_shipments', $this->password_for_shipments);
          if($stmt->execute()){

         $stmtq = $this->dbConn->prepare("SELECT item_name, item_weight, item_length, item_width, item_category FROM `all_shipments_items` WHERE waybill_number = :waybill_numberaa");
         $stmtq->bindParam(':waybill_numberaa', $this->waybill_number);                 
         $stmtq->execute(); 

            $customers[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $customers[] = $stmtq->fetchAll(PDO::FETCH_ASSOC);

             return $customers;

print_r($customers);
exit();

它返回

  Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [payment_method] => 1
                    [date_of_delivery] => B
                    [set_date_of_delivery] => 2018-06-28 00:00:00
                    [waybill_number] => 333333333
                    [shipment_type] => INTERNATIONAL
                    [country_from] => NGA
                    [state_from] => Lagos
                    [area_from] => Mushin
                    [country_to] => ZAF
                    [state_to] => Crea
                    [area_to] => Brooklyn
                    [street_address_to] => 14 Oladosun
                    [name_of_shipment_owner] => Oshe man
                    [email_of_shipment_owner] => chiefoazubike@gmail.com
                    [phone_of_shipment_owner] => 08139615325
                    [phone_of_shipment_owner_1] => 08023039112
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [item_name] => Fish carton
                    [item_weight] => 30
                    [item_length] => 20
                    [item_width] => 14
                    [item_category] => H
                )

            [1] => Array
                (
                    [item_name] => Fish carton
                    [item_weight] => 30
                    [item_length] => 20
                    [item_width] => 14
                    [item_category] => H
                )

        )

)

当我分配对象且其值低于

          $response['payment_type'] = $customers['payment_method']; 
      $response['date_of_delivery'] = $customers['date_of_delivery'];  
      $response['actual_date_of_delivery'] = $customers['set_date_of_delivery'];
      $response['your_generated_waybill_number'] = $customers['waybill_number'];
      $response['shipment_type'] = $customers['shipment_type'];
      $response['country_from'] = $customers['country_from'];
      $response['state_from'] = $customers['state_from'];
      $response['area_from'] = $customers['area_from'];
      $response['country_to'] = $customers['country_to'];
      $response['state_to'] = $customers['state_to'];
      $response['area_to'] = $customers['area_to'];
      $response['street_address_to'] = $customers['street_address_to'];
      $response['name_of_shipment_owner'] = $customers['name_of_shipment_owner'];
      $response['email_of_shipment_owner'] = $customers['email_of_shipment_owner'];
      $response['phone_number_of_shipment_owner'] = $customers['phone_of_shipment_owner'];
      $response['phone_number_of_shipment_owner_1'] = $customers['phone_of_shipment_owner_1'];
      $response['name'] = $customers['item_name'];
      $response['actual_weight'] = $customers['item_weight'];
      $response['width'] = $customers['item_length'];
      $response['length'] = $customers['item_width'];
      $response['category'] = $customers['item_category'];

      $this->returnResponse(SUCCESS_RESPONSE, $response);

然后发送$this->returnResponse(SUCCESS_RESPONSE, $response);,它以

回应
      {
"response": {
    "status": 200,
    "message": {
        "payment_type": null,
        "date_of_delivery": null,
        "actual_date_of_delivery": null,
        "your_generated_waybill_number": null,
        "shipment_type": null,
        "country_from": null,
        "state_from": null,
        "area_from": null,
        "country_to": null,
        "state_to": null,
        "area_to": null,
        "street_address_to": null,
        "name_of_shipment_owner": null,
        "email_of_shipment_owner": null,
        "phone_number_of_shipment_owner": null,
        "phone_number_of_shipment_owner_1": null,
        "name": null,
        "actual_weight": null,
        "width": null,
        "length": null,
        "category": null
    }
}

}


        //rest.php
            public function returnResponse($code, $data){
    header("content-type: application/json");
    $response = json_encode(['response' => ['status' => $code, "message"  => $data]]);
    echo $response; exit;       

}

1 个答案:

答案 0 :(得分:3)

由于没有分配null的正确元素,所以得到$customers

// you are using this
$response['payment_type'] = $customers['payment_method']; 
$response['actual_weight'] = $customers['item_weight'];

// but this is where you value is
$response['payment_type'] = $customers[0][0]['payment_method']; 
$response['actual_weight'] = $customers[1][0]['item_weight'];
// AND your second item
$response['actual_weight'] = $customers[1][1]['item_weight'];