www.webdeveloper.com
Results 1 to 6 of 6

Thread: Another JSON PHP question

  1. #1
    Join Date
    Jul 2012
    Posts
    3

    Another JSON PHP question

    Having a bit of confusion with this.

    Using this test received JSON result (there is only 1 result in this example but other uses might have multiple results)

    {
    "success": true,
    "numResults": 1,
    "execTimeMs": 10,
    "results":
    [
    {
    {
    "TestAccount": "testingaccount1",
    "testnotes": {
    "test_department": "creative",
    "test_office_location": "98th Floor"
    },
    "testdescription": "creative test description\n \n",
    "testaccountcat": "intl",
    "deptimages":
    [
    "http://www.test.com/creativedeptlogo.jpg",
    "http://www.test.com/creativedepthead.jpg.jpg"
    ],
    "officenumber": 8531212
    }
    ]
    }

    trying to figure out a way to have all of these items assigned as PHP variables for another script that would insert them into a MYSQL database.

    So far have this

    $url = "[we would have the url where the JSON data is available here that would produce the output seen in the example above]"
    $json = file_get_contents($url);
    $out = json_decode($json,true);

    If I output $out to a file, I get this
    Array
    (
    [success] => 1
    [numResults] => 1
    [execTimeMs] => 10
    [results] => Array
    (
    [0] => Array
    (
    [testAccount] => testingaccount1
    [testnotes] => Array
    (
    [test_department] => creative
    [test_office_location] => 98th Floor
    )

    [testdescription] =>
    creative test description\n \n
    [testaccountcat] => intl
    [deptimages] => Array
    (
    [0] => http://www.test.com/creativedeptlogo.jpg

    [1] => http://www.test.com/creativedepthead.jpg.
    )

    [officenumber] => 8531212
    )

    )

    )

    Thanks in advance for any assistance

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    Not sure what the exact problem is?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2012
    Posts
    3
    Quote Originally Posted by NogDog View Post
    Not sure what the exact problem is?
    Trying to figure out how to assign each of the decoded JSON items from the array as variables with PHP so they can be inserted into a MYSQL table.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    They are already in array variables, so there is really no need to assign them to other variables. Since you did not wrap your output in [code]...[/code] tags here, it's difficult to see the hierarchy/nesting; but for example, if you want to reference the "testingaccount1" value, it would appear to be:
    PHP Code:
    $out['results'][0]['testAccount'
    .
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2012
    Posts
    3
    Sorry, here is the items in code brackets

    Using this test received JSON result (there is only 1 result in this example but other uses might have multiple results)

    Code:
    {
    "success": true,
    "numResults": 1,
    "execTimeMs": 10,
    "results":
    [
    {
    {
    "TestAccount": "testingaccount1",
    "testnotes": {
    "test_department": "creative",
    "test_office_location": "98th Floor"
    },
    "testdescription": "creative test description\n \n",
    "testaccountcat": "intl",
    "deptimages":
    [
    "http://www.test.com/creativedeptlogo.jpg",
    "http://www.test.com/creativedepthead.jpg.jpg"
    ],
    "officenumber": 8531212
    }
    ]
    }
    trying to figure out a way to have all of these items assigned as PHP variables for another script that would insert them into a MYSQL database.

    So far have this
    PHP Code:
    $url "[we would have the url where the JSON data is available here that would produce the output seen in the example above]"
    $json file_get_contents($url);
    $out json_decode($json,true); 
    If I output $out to a file, I get this

    Code:
    Array
    (
    [success] => 1
    [numResults] => 1
    [execTimeMs] => 10
    [results] => Array
    (
    [0] => Array
    (
    [testAccount] => testingaccount1
    [testnotes] => Array
    (
    [test_department] => creative
    [test_office_location] => 98th Floor
    )
    
    [testdescription] =>
    creative test description\n \n
    [testaccountcat] => intl
    [deptimages] => Array
    (
    [0] => http://www.test.com/creativedeptlogo.jpg
    
    [1] => http://www.test.com/creativedepthead.jpg.
    )
    
    [officenumber] => 8531212
    )
    
    )
    
    )
    So I saw your comments

    They are already in array variables, so there is really no need to assign them to other variables.
    but for example, if you want to reference the "testingaccount1" value, it would appear to be:
    PHP Code:
    $out['results'][0]['testAccount'
    I understand what you are saying there. I should have used the multiple result example as it is more relevant to the point of my question - so I'll provide and example output for $out with a multiple result JSON decode

    Code:
                [0] => Array
                    (                    
                        [testAccount] => testingaccount1
                        [testnotes] => Array
                            (
                                [test_department] => creative
                                [test_office_location] => 98th Floor                            
                            )
    
                        [testdescription] => 
    creative test description\n \n
    
                        [testaccountcat] => intl                  
                        [deptimages] => Array
                            (
                                [0] => http://www.test.com/creativedeptlogo.jpg
                                [1] =>  http://www.test.com/creativedepthead.jpg
                                
                            )
                       
                        [officenumber] => 8531212
                    )
    
                [1] => Array
                    (                    
                        [testAccount] => testingaccount2
                        [testnotes] => Array
                            (
                                [test_department] => security
                                [test_office_location] => 97th Floor                            
                            )
    
                        [testdescription] => 
    security test description\n \n
    
                        [testaccountcat] => govt                  
                        [deptimages] => Array
                            (
                                [0] => http://www.test.com/securitydeptlogo.jpg
                                [1] =>  http://www.test.com/securitydepthead.jpg
                                
                            )
                       
                        [officenumber] => 8765309
                    )
    
            )
    
    )
    So when we have these decoded JSON results, there may be anywhere from 1 to 200 results. So each individual result set starts with

    Code:
    [0] => Array 
    
    then the next result set is
    [1] => Array 
    
    and the next
    [2] => Array 
    
    and so on.....
    We need to take each of the items from each result set and inject them into a MYSQL Table that contains matching fields. Just not sure the easiest way to do this since the result set numbers change and the number of results can be different each time.

    Thanks again.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    Sounds like a job for a foreach() loop.
    PHP Code:
    foreach($out['results'] as $record)
    {
        echo 
    $record['testAccount'];
        echo 
    $record['testnotes']['test_department'];
        echo 
    $record['testnotes']['test_office_location'];

    (Obviously you want to do something other than just echo stuff in the loop. )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles