0

The 'shipping_address' is displaying an error "Notice: Array to string conversion", I have this line of code:

EDIT -- Removed code snippet and replacing it with the full code connecting to BigCommerce store

<?php
require 'vendor/autoload.php';
use Bigcommerce\Api\Client as Bigcommerce;
Bigcommerce::configure(array(
    'store_url' => 'xxx',
    'username'  => 'xxx',
    'api_key'   => 'xxx'
));
$ping = Bigcommerce::getTime();


$orders = Bigcommerce::getOrders();
$data = json_decode($shipping_address, true);
foreach ($orders as $orders) {

     echo "<table border='1' width='100%' cellspacing='0' cellpadding='5' bordercolor='#000' style='border-bottom:0;'>
        <tr>
        <th width='15%'>Barcode</th>
        <th width='7%'>Order date</th>
        <th width='23%'>Customer details</th>
        <th width='8%'>Phone #</th>
        <th width='22%'>Total</th>
        <th width='15%'>Comments</th>
        <th width='10%'>Signature</th>
        </tr>";


    echo "<tr>";
    echo "<td style='text-align:center;'>" $orders->id . "</td>";
    echo "<td>" . $orders->date_created . "</td>";
    echo "<td>" . $shipping_address = $orders['shipping_address'][0]['first_name'] . "</td>";
     echo "<td>" . $orders->phone . "</td>";
    echo "<td style='text-align:center;'>" . $orders->total_inc_tax . "</td>";
    echo "<td>" . $orders->customer_message . "</td>";
     echo "<td>" . "</td>";
    echo "</tr>";
    echo "</table>";

}

?>
5
  • $orders->shipping_addresses is array, not string Commented Oct 25, 2017 at 7:20
  • 2
    Possible duplicate of access json object and array Commented Oct 25, 2017 at 7:20
  • Possible duplicate of PHP Array to string conversion error Commented Oct 25, 2017 at 7:21
  • use foreach() for $orders->shipping_addresses values Commented Oct 25, 2017 at 7:24
  • How would you like to display the shipping_addresses array in the column? Commented Oct 25, 2017 at 8:02

3 Answers 3

0

you can't display an array directly, instead, you can display it like this:

<td>
<?php foreach($orders->shipping_addresses as $address): ?>
"first_name": <?=$address['first_name']?>
"last_name": <?=$address['last_name']?>
"company": <?=$address['company']?>
"street_1": <?=$address['street_1']?>
"street_2": <?=$address['street_2']?>
"phone": <?=$address['phone']?>
"email": <?=$address['email']?>
<br />
<?php endforeach; ?> 
</td>

of course, you should decode json using:

 json_decode($json, true);
Sign up to request clarification or add additional context in comments.

1 Comment

How can I echo this?
0

Will you determinate variable $address for address above. For example:

$address = 'Street1:' . $orders->shipping_addresses['street_1'] . '<br>Street2' . $orders->shipping_addresses['street_2'];

Also, you can use this inline

echo "<td>" . $orders->shipping_addresses['street_1'] . "</td>";

In case $orders->shipping_addresses was decoded json.

json_decode($shipping_addresses, true); 

Comments

0

If this is what you want in the column

John Doe
Company
Street 1
Street 2
+1234567890
[email protected]

Try

$orders = Bigcommerce::getOrders();
$orders_array = json_decode($orders, true);

echo $orders[0]['shipping_address']['first_name'];

2 Comments

Hi Kenneth, I'm getting undefined index for first_name etc, and variable errors with 'address'
This line $data = json_decode($shipping_address, true); is invalid since you have not declared $shipping_address anywhere

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.