Jump to content

Please help with PHP arrays


shingionline

Recommended Posts

Hi Guys,

 

I have an array

$arr

 

When i run this function

print_r($arr)

 

I get the output below:-

 

--------------------------------

 

Array
(
    [WHMCSAPI] => Array
        (
            [ACTION] => getclientsproducts
            [RESULT] => success
            [CLIENTID] => 87
            [PID] => 13
            [DOMAIN] => dfgbhcgnd.com
            [TOTALRESULTS] => 1
            [sTARTNUMBER] => 0
            [NUMRETURNED] => 1
            [PRODUCTS] => Array
                (
                    [PRODUCT] => Array
                        (
                            [iD] => 196
                            [CLIENTID] => 87
                            [ORDERID] => 218
                            [PID] => 13
                            [REGDATE] => 2011-12-07
                            [NAME] => Walderizer
                            [DOMAIN] => dfgbhcgnd.com
                            [DEDICATEDIP] => 
                            [sERVERID] => 3
                            [FIRSTPAYMENTAMOUNT] => 55.00
                            [RECURRINGAMOUNT] => 55.00
                            [PAYMENTMETHOD] => banktransfer
                            [PAYMENTMETHODNAME] => Bank Transfer
                            [bILLINGCYCLE] => Monthly
                            [NEXTDUEDATE] => 2011-12-07
                            [sTATUS] => Active
                            [uSERNAME] => vitaforiz
                            [PASSWORD] => ghTfg476fg
                            [sUBSCRIPTIONID] => 
                            [LASTUPDATE] => 0000-00-00 00:00:00
                            [CUSTOMFIELDS] => Array
                                (
                                    [CUSTOMFIELD] => Array
                                        (
                                            [NAME] => IP Address
                                            [VALUE] => 
                                        )

                                )

                            [CONFIGOPTIONS] => 

                        )

                )

        )

)

 

---------------------------------

 

My question is, how can i print only certain parts of the data rather than printing the whole array. For example i only want to echo the values for [NAME], [uSERNAME], [PASSWORD] and [NEXTDUEDATE] from the [PRODUCT] part of the array

 

Thanks in advance

Link to comment
Share on other sites

I was going to give you the easy answer, but I'm very sure that it won't work in all circumstances.

 

Comes from XML, right? The problem is that there might be multiple PRODUCTs inside that PRODUCTS, but because PHP doesn't know that it assumes (in this instance) that there's only one. The array will look different in the two cases. However there's that NUMRETURNED you can use to get a proper array.

$products = ($arr["WHMCSAPI"]["NUMRETURNED"] == 1 ? array($arr["WHMCSAPI"]["PRODUCTS"]["PRODUCT"]) : $arr["WHMCSAPI"]["PRODUCTS"]);
foreach ($products as $p) {
    echo "Name: ", $p["NAME"], "
\n";
}

Link to comment
Share on other sites

You can do this by echoing them.

 

e.g. to echo the value for "result"

 


echo $arr[WHMCSAPI][RESULT];

 

Thanks, this works well for the first level eg:

echo $arr['WHMCSAPI']['DOMAIN'];

 

How can i get to the [uSERNAME] which is on the second level. The code below 

echo $arr['WHMCSAPI']['PRODUCT']['USERNAME'];

gives the error message "Undefined index: PRODUCT"

Link to comment
Share on other sites

I was going to give you the easy answer, but I'm very sure that it won't work in all circumstances.

 

Comes from XML, right? The problem is that there might be multiple PRODUCTs inside that PRODUCTS, but because PHP doesn't know that it assumes (in this instance) that there's only one. The array will look different in the two cases. However there's that NUMRETURNED you can use to get a proper array.

$products = ($arr["WHMCSAPI"]["NUMRETURNED"] == 1 ? array($arr["WHMCSAPI"]["PRODUCTS"]["PRODUCT"]) : $arr["WHMCSAPI"]["PRODUCTS"]);
foreach ($products as $p) {
    echo "Name: ", $p["NAME"], "<br />\n";
}

 

Thanks for your help, yes this is coming from XML. The simple solution will be fine, there will only ever be 1 PRODUCT. The code below is fine for first level values

echo $arr['WHMCSAPI']['DOMAIN'];

but when i try to get [uSERNAME] with

echo $arr['WHMCSAPI']['PRODUCT']['USERNAME'];

i get the error "Undefined index: PRODUCT"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.