代码之家  ›  专栏  ›  技术社区  ›  Shasha

JSON数组对象值返回空值

  •  2
  • Shasha  · 技术社区  · 8 年前

    我不知道我似乎做错了什么,当我试图用json显示一个数组时,所有对象的值都返回null,但是当我 print_r array它完美地返回数组,但是当我试图用php显示代码时,它返回值为空我的代码,

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

    当我运行查询并打印输出时 $客户

     //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 回复  |  直到 8 年前
        1
  •  3
  •   Mr Glass    8 年前

    你得到了 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'];