Click here to Skip to main content
15,937,429 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have the following data structure but i can't figure out how to access the order below orders. I tried 1 & 2 to use nested foreach loops and any solution that i could find but i only get the first 4 values (success101). It's for the WHMCS application and the data structure is also documented here.
What am i doing wrong or could it be that it's blocked within the application?


My data structure:

PHP
{"data":{"result":"success","totalresults":1,"startnumber":0,"numreturned":1,"orders":{"order":[{"id":6,"ordernum":8015255572,"userid":3,"contactid":0,"requestor_id":0,"admin_requestor_id":2,"date":"2021-11-05 13:21:32","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"0.00","paymentmethod":"banktransfer","invoiceid":3,"status":"Pending","ipaddress":"xxx","fraudmodule":"","fraudoutput":"","notes":"","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"Emma Test","currencyprefix":"\u20ac","currencysuffix":" EUR","frauddata":"","validationdata":"","lineitems":{"lineitem":[{"type":"product","relid":9,"producttype":"Other Product\/Service","product":"xxx & Exchange - xxx","domain":"xxx","billingcycle":"Free Account","amount":{},"status":"Pending"},{"type":"addon","relid":3,"producttype":"Addon","product":"Exchange Postfach","domain":"","billingcycle":"Free Account","amount":{},"status":"Pending"}]}}]}}}


Regards

What I have tried:

1. $test= json_encode(array('data' => $results));
2. $test= (array) $results;

Shouldn't it work?

PHP
foreach($test as $item)
    {
        fputs($f, $item);
            foreach($item as $orders)
            {
                foreach($orders as $order)
                {
                    fputs($f, $order);
                }
            }
    }
Posted
Updated 1-Jan-23 19:43pm
v2
Comments
Chris Copeland 11-Nov-21 4:13am    
You've provided an example of a JSON payload, is this how it's stored in the PHP? If it's stored as a string you'll need to use json_decode() which takes a JSON string and converts it into an object. Then you can access the properties of the JSON accordingly.

Also remember that this JSON decoded would be an object so you need to reference the properties of said object. Ie. $object->data->orders->order
huhuhhuhuh 12-Nov-21 5:03am    
Hi,
Yes, i'm getting the payload with the following code within php (in WHMCS it's a hook in php):

$command = 'GetOrders';
$postData = array(
'id' => '1',
);
$adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later

$results = localAPI($command, $postData, $adminUsername);

The following works, if i test it on the following site but it doesn't work within the WHMCS app, when i decode the $results variable. Why? (It's php 7.4.16 on the server):
https://sandbox.onlinephpfunctions.com/

$obj = '{"result":"success","totalresults":1,"startnumber":0,"numreturned":1,"orders":{"order":[{"id":6,"ordernum":8015255572,"userid":3,"contactid":0,"requestor_id":0,"admin_requestor_id":2,"date":"2021-11-05 13:21:32","nameservers":"","transfersecret":"","renewals":"","promocode":"","promotype":"","promovalue":"","orderdata":"[]","amount":"0.00","paymentmethod":"banktransfer","invoiceid":3,"status":"Pending","ipaddress":"xxx","fraudmodule":"","fraudoutput":"","notes":"","paymentmethodname":"Bank Transfer","paymentstatus":"Paid","name":"Emma Test","currencyprefix":"\u20ac","currencysuffix":" EUR","frauddata":"","validationdata":"","lineitems":{"lineitem":[{"type":"product","relid":9,"producttype":"Other Product\/Service","product":"xxx","domain":"xxx","billingcycle":"Free Account","amount":{},"status":"Pending"},{"type":"addon","relid":3,"producttype":"Addon","product":"Exchange Postfach","domain":"","billingcycle":"Free Account","amount":{},"status":"Pending"}]}}]}}';

$decoded = json_decode($obj,true);


echo $decoded['orders']['order'][0]['ordernum'];

1 solution

JSON is a programming language independent data transfer format, that works with pretty much any language which supports objects (and some that don't).
If you feed your JSON data sample into a converter (there is one here: Convert JSON to PHP Array Online | WTOOLS[^] ) you get the following PHP code:
PHP
array (
  'data' => 
  array (
    'result' => 'success',
    'totalresults' => 1,
    'startnumber' => 0,
    'numreturned' => 1,
    'orders' => 
    array (
      'order' => 
      array (
        0 => 
        array (
          'id' => 6,
          'ordernum' => 8015255572,
          'userid' => 3,
          'contactid' => 0,
          'requestor_id' => 0,
          'admin_requestor_id' => 2,
          'date' => '2021-11-05 13:21:32',
          'nameservers' => '',
          'transfersecret' => '',
          'renewals' => '',
          'promocode' => '',
          'promotype' => '',
          'promovalue' => '',
          'orderdata' => '[]',
          'amount' => '0.00',
          'paymentmethod' => 'banktransfer',
          'invoiceid' => 3,
          'status' => 'Pending',
          'ipaddress' => 'xxx',
          'fraudmodule' => '',
          'fraudoutput' => '',
          'notes' => '',
          'paymentmethodname' => 'Bank Transfer',
          'paymentstatus' => 'Paid',
          'name' => 'Emma Test',
          'currencyprefix' => '€',
          'currencysuffix' => ' EUR',
          'frauddata' => '',
          'validationdata' => '',
          'lineitems' => 
          array (
            'lineitem' => 
            array (
              0 => 
              array (
                'type' => 'product',
                'relid' => 9,
                'producttype' => 'Other Product/Service',
                'product' => 'xxx & Exchange - xxx',
                'domain' => 'xxx',
                'billingcycle' => 'Free Account',
                'amount' => 
                array (
                ),
                'status' => 'Pending',
              ),
              1 => 
              array (
                'type' => 'addon',
                'relid' => 3,
                'producttype' => 'Addon',
                'product' => 'Exchange Postfach',
                'domain' => '',
                'billingcycle' => 'Free Account',
                'amount' => 
                array (
                ),
                'status' => 'Pending',
              ),
            ),
          ),
        ),
      ),
    ),
  ),
)
Which shows you the structure of your data and how you would access it.

Then - as Chris suggested - use PHP: json_decode - Manual[^] to convert your actual data string to that structure and you can access any part of the data as if the code I showed was written into your app.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900