shingionline Posted December 7, 2011 Share Posted December 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252699-please-help-with-php-arrays/ Share on other sites More sharing options...
Drongo_III Posted December 7, 2011 Share Posted December 7, 2011 You can do this by echoing them. e.g. to echo the value for "result" echo $arr[WHMCSAPI][RESULT]; Quote Link to comment https://forums.phpfreaks.com/topic/252699-please-help-with-php-arrays/#findComment-1295453 Share on other sites More sharing options...
requinix Posted December 7, 2011 Share Posted December 7, 2011 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/252699-please-help-with-php-arrays/#findComment-1295455 Share on other sites More sharing options...
shingionline Posted December 7, 2011 Author Share Posted December 7, 2011 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" Quote Link to comment https://forums.phpfreaks.com/topic/252699-please-help-with-php-arrays/#findComment-1295463 Share on other sites More sharing options...
shingionline Posted December 7, 2011 Author Share Posted December 7, 2011 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" Quote Link to comment https://forums.phpfreaks.com/topic/252699-please-help-with-php-arrays/#findComment-1295469 Share on other sites More sharing options...
requinix Posted December 7, 2011 Share Posted December 7, 2011 You're leaving out the PRODUCTS layer. Quote Link to comment https://forums.phpfreaks.com/topic/252699-please-help-with-php-arrays/#findComment-1295490 Share on other sites More sharing options...
shingionline Posted December 7, 2011 Author Share Posted December 7, 2011 You're leaving out the PRODUCTS layer. Got it! Thank you very much guys, everything is working perfectly Quote Link to comment https://forums.phpfreaks.com/topic/252699-please-help-with-php-arrays/#findComment-1295493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.