simonp Posted May 5, 2015 Share Posted May 5, 2015 Hi, I've got this JSON output: {"result":"success","clientid":null,"serviceid":null,"pid":null,"domain":"testdomain.com","totalresults":"1","startnumber":0,"numreturned":1,"products":{"product":[{"id":"3804","clientid":"1401","orderid":"3276","pid":"53","regdate":"2010-02-14","name":"Basic","groupname":"Plans","domain":"testdomain.com","dedicatedip":"","serverid":"4","servername":"serverx.com","serverip":"123.123.123.123","serverhostname":"","firstpaymentamount":"7.99","recurringamount":"7.99","paymentmethod":"wibble","paymentmethodname":"Credit\/Debit Card","billingcycle":"Monthly","nextduedate":"2010-02-14","status":"Terminated","username":"dcdqw","password":"z1nmY1p4Y9","subscriptionid":"","promoid":"0","overideautosuspend":"","overidesuspenduntil":"0000-00-00","ns1":"","ns2":"","assignedips":"","notes":"","diskusage":"0","disklimit":"1000","bwusage":"0","bwlimit":"50000","lastupdate":"2015-05-01 06:01:17","customfields":{"customfield":[{"id":"36","name":"ThisUserID","value":"1243214"}]},"configoptions":{"configoption":[]}}]}} ... and I'm trying to get the value for ThisUserID but I'm struggling so much with the arrays! I've copied it into http://jsonviewer.stack.hu so I can understand it more but just can't get that value. I understand: $clientid = $arr->clientid; ... but can't work out how to drill deeper. Any help appreciated Quote Link to comment https://forums.phpfreaks.com/topic/296084-how-do-i-get-this-variable-from-json/ Share on other sites More sharing options...
Solution requinix Posted May 5, 2015 Solution Share Posted May 5, 2015 (edited) ->clientid is null so that's not it. [edit] Oh, the general syntax? Yes, if you decode JSON to an object (the default) then you'd use lots of ->s. [/edit] Starting at ThisUserID and working backwards, - That's the value and the key is "name" - "name" is part of an object in the "customfield" array - "customfield" is an array in the "customfields" object - "customfields" is part of an object in the "product" array - "product" is an array in the "products" object - "products" is at the top level Reversing that gives you the path. $arr->products->product[0]->customfields->customfield[0]->nameThere are two arrays in there. You need to consider whether they will have any objects inside them at all, whether there is only ever one, or whether there could be more than one. Edited May 5, 2015 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/296084-how-do-i-get-this-variable-from-json/#findComment-1510854 Share on other sites More sharing options...
simonp Posted May 5, 2015 Author Share Posted May 5, 2015 Thanks so much requinx - that's really helpful. I'd got so close but didn't quite master the [0] part! Quote Link to comment https://forums.phpfreaks.com/topic/296084-how-do-i-get-this-variable-from-json/#findComment-1510855 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.