Formatting API Results to an Array?

Discussion in 'PHP' started by blackburn2413, Nov 20, 2015.

  1. #1
    I have an API that I am using that is essentially returning data you would expect to see in array, but in a format where it is one long string of information. It looks a bit like this:

    {"results":[{"NodeID":1,"Icon":"9.gif ","DisplayName":"Device 1"},{"NodeID":2,"Icon":"9.gif ","DisplayName":"Device 2"},{"NodeID":3,"Icon":"9.gif ","DisplayName":"Device3"}]}

    I have a site written in basic php and is using "file_get_contents" with a formatted url to get this info. Is there any way that I can format this string into an array with alternating values for NodeID, Icon, and DisplayName? Essentially something like this for the above string:

    "1, 9.gif, Device1, 2, 9.gif, Device2, 3, 9.gif, Device3"

    Any ideas? Thank you in advance!
     
    blackburn2413, Nov 20, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    I'm pretty sure that's not "a long string of information" but a JSON-encoded result.

    Just do a $result = json_decode($data,true); to get an associative array for the values (you'll then access it something like $result[0][NodeID] and so forth.
     
    PoPSiCLe, Nov 20, 2015 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    That is indeed JSON encoded data, it makes a lot more sense if you format it.

    {
    	"results"  :  [
    		{
    			"NodeID" : 1,
    			"Icon" : "9.gif ",
    			"DisplayName" : "Device 1"
    		}, {
    			"NodeID" : 2,
    			"Icon" : "9.gif ",
    			"DisplayName" : "Device 2"
    		},{
    			"NodeID" : 3,
    			"Icon" : "9.gif ",
    			"DisplayName" : "Device3"
    		}
    	]
    }
    Code (markup):
    and a json_decode of it:
    http://php.net/manual/en/function.json-decode.php

    Would give you this array structure:

    stdClass Object
    (
        [results] => Array
            (
                [0] => stdClass Object
                    (
                        [NodeID] => 1
                        [Icon] => 9.gif 
                        [DisplayName] => Device 1
                    )
    
                [1] => stdClass Object
                    (
                        [NodeID] => 2
                        [Icon] => 9.gif 
                        [DisplayName] => Device 2
                    )
    
                [2] => stdClass Object
                    (
                        [NodeID] => 3
                        [Icon] => 9.gif 
                        [DisplayName] => Device3
                    )
    
            )
    
    )
    Code (markup):
    So for example if you did:

    $test = json_decode($jsonString);

    Where $jsonString contained that data from your post, $test['results'][2]['DisplayName'] would be 'Device 3'.
     
    deathshadow, Nov 21, 2015 IP
    KangBroke likes this.