简体   繁体   中英

JSON array object value returning null

I don't know what I seem to be doing wrong, when I try to show an array with JSON the value returns null on all objects, but when I print_r array it returns the array perfectly, but when I try to display that code with php, it returns value as null my code,

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

WHEN i run my query and print_r out $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();

it returns

  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
                )

        )

)

When I assign objects and with value below

          $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);

and then send $this->returnResponse(SUCCESS_RESPONSE, $response); out, it responds with

      {
"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;       

}

You are getting null because you not assigning the correct elements of $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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM