Home > Software design >  Im trying to access an array / object element in JSON code? I tried but nothing is displaying proper
Im trying to access an array / object element in JSON code? I tried but nothing is displaying proper

Time:04-04

        [shipping] => stdClass Object
            (
                [first_name] => ######
                [last_name] => ######
                [address_1] => ######
                [address_2] => ######
                [city] =>######
                [state] => ######
                [postcode] => ######
                [country] => ######
            )

        [payment_method] => bacs
        [payment_method_title] => Manual EFT/ Direct Deposit
        [transaction_id] => 
        [date_paid] => 2022-03-28T06:59:18
        [date_paid_gmt] => 2022-03-28T04:59:18
        [date_completed] => 2022-03-28T08:56:39
        [date_completed_gmt] => 2022-03-28T06:56:39
        [cart_hash] => 06b1f426278d37220edc0621c1682bcd
        [meta_data] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 133516
                        [key] => _billing_same_as_shipping
                        [value] => 
                    )

                [1] => stdClass Object
                    (
                        [id] => 133517
                        [key] => _shipping_calculator
                        [value] => 
                    )

                [2] => stdClass Object
                    (
                        [id] => 133518
                        [key] => is_vat_exempt
                        [value] => no
                    )

                [3] => stdClass Object
                    (
                        [id] => 133519
                        [key] => _order_number
                        [value] => 5550050071
                    )

                [4] => stdClass Object
                    (
                        [id] => 133520
                        [key] => _order_number_formatted
                        [value] => CRO5550050071
                    )

                [5] => stdClass Object
                    (
                        [id] => 133521
                        [key] => _order_number_meta
                        [value] => stdClass Object
                            (
                                [prefix] => CRO
                                [suffix] => 
                                [length] => 10
                            )

                    )

                [6] => stdClass Object
                    (
                        [id] => 133522
                        [key] => _wc_facebook_for_woocommerce_order_placed
                        [value] => yes
                    )

                [7] => stdClass Object
                    (
                        [id] => 133523
                        [key] => ywot_tracking_code
                        [value] => CR
                    )

                [8] => stdClass Object
                    (
                        [id] => 133524
                        [key] => ywot_tracking_postcode
                        [value] => 
                    )

                [9] => stdClass Object
                    (
                        [id] => 133525
                        [key] => ywot
                        [value] => D
                    )

                [10] => stdClass Object
                    (
                        [id] => 133526
                        [key] => ywot_da
                        [value] => 
                    )

                [11] => stdClass Object
                    (
                        [id] => 133527
                        [key] => ywot
                        [value] => 
                    )

                [12] => stdClass Object
                    (
                        [id] => 133530
                        [key] => _wfacp_
                        [value] => h
                    )

                [13] => stdClass Object
                    (
                        [id] => 133531
                        [key] => _wfacp_post_id
                        [value] => 90999999238378
                    )

                [14] => stdClass Object
                    (
                        [id] => 133532
                        [key] => order_comments
                        [value] => 
                    )

                [15] => stdClass Object
                    (
                        [id] => 133536
                        [key] => _ywpi_invoice_number
                        [value] => 61
                    )

                [16] => stdClass Object
                    (
                        [id] => 133537
                        [key] => _ywpi_invoiced
                        [value] => 1
                    )

                [17] => stdClass Object
                    (
                        [id] => 133538
                        [key] => _ywpi_inv
                        [value] => CRO
                    )

                [18] => stdClass Object
                    (
                        [id] => 133539
                        [key] => _ywpi_nvoi=
                        [value] => 
                    )

                [19] => stdClass Object
                    (
                        [id] => 133540
                        [key] => _ywpi_i
                        [value] => 61
                    )

                [20] => stdClass Object
                    (
                        [id] => 133541
                        [key] => _ywpi_inv
                        [value] => CO5
                    )

                [21] => stdClass Object
                    (
                        [id] => 133542
                        [key] => _ywpi
                        [value] => 2022-03-25 16:59:25
                    )

                [22] => stdClass Object
                    (
                        [id] => 133543
                        [key] => _ywpi_invoice_path
                        [value] => Invoice_61.pdf
                    )

                [23] => stdClass Object
                    (
                        [id] => 133544
                        [key] => _ywpi_invoice_folder
                        [value] => Invoices
                    )

                [24] => stdClass Object
                    (
                        [id] => 133545
                        [key] => _ywpi_has_packing_slip
                        [value] => 1
                    )

                [25] => stdClass Object
                    (
                        [id] => 133546
                        [key] => _ywpi_pack
                        [value] => Ship
                    )

                [26] => stdClass Object
                    (
                        [id] => 133547
                        [key] => _ywpi_packider
                        [value] => Invoices
                    )

                [27] => stdClass Object
                    (
                        [id] => 133548
                        [key] => _ga_t
                        [value] => 1
                    )

                [28] => stdClass Object
                    (
                        [id] => 133549
                        [key] => _wfacebour
                        [value] => yes
                    )

                [29] => stdClass Object
                    (
                        [id] => 133680
                        [key] => _woofunnel_cid
                        [value] => 7
                    )

                [30] => stdClass Object
                    (
                        [id] => 133685
                        [key] => dawnwing_api_response
                        [value] => {"data":["http://swatws.dawnwing.co.za/dwwebservices/v2/live/labels/CRO55503_CR50071_1.pdf"],"isSuccess":true,"hasError":false,"message":"Waybill completed successfully - CollectionNo : ","exception":null}
                    )

                [31] => stdClass Object
                    (
                        [id] => 133686
                        [key] => dawnwing_labels
                        [value] => ["http://swatws.dawnwing.co.za/dwwebservices/v2/live/labels/CRO55503_CR50071_1.pdf"]
                    )
            )

This is part I would like to display [31] => stdClass Object ( [id] => 133686 [key] => dawnwing_labels [value] => ["http://swatws.dawnwing.co.za/dwwebservices/v2/live/labels/CRO55503_CR50071_1.pdf"] ) )

My code looks like this : I want to display that object / array inside the table row for each order. For every order there is a unique waybill link what I want to accomplish is to display that link inside my datatable whenever and order is updated and the waybill is generated.

I hope that makes sense.

 <?php
           foreach($results as $details){
               // This line converts the stdClass into an array
               $details_array = json_decode(json_encode($details), true);
               
               // use $details_array to access the values from array
           echo "<tr>
                    <td>" . $details_array["number"]."</td>
                     <td>" . $details_array["billing"]["first_name"].$details_array["billing"]["last_name"]."</td>
                     <td>" . $details_array["shipping"]["address_1"]."</td>
                     <td>" . $details_array["billing"]["phone"]."</td>
                     <td>" . $details_array["date_created"]."</td>
                     <td>" . $details_array["status"]."</td>
                     <td>" . $details_array["total"]."</td>
                     <td>" 
                     .$details_array["total"]."</td>
                     <td><a class='open-AddBookDialog btn btn-primary' data-target='#myModal' data-id=".$details_array['id']." data-toggle='modal'>Update</a>
                     <a class='open-deleteDialog btn btn-danger' data-target='#myModal1' data-id=".$details_array['id']." data-toggle='modal'>Delete</a>
                            
                        
                        <a value='Download' onclick='DownloadJSON()'class=' btn btn-success'>Download</a>
                        

                     </td></tr>";
                         }
                         ?>

CodePudding user response:

Loop over the metadata to get the URL you want.

$waybill_link = '';
foreach ($details_array['meta_data'] as $item) { 
    if ($item['key'] == 'dawnwing_labels') {
        $waybill_link = json_decode($item['value'], true)[0];
        break;
    }
}

Then you can display a link with $waybill_link in the href.

  • Related