/    Sign up×
Community /Pin to ProfileBookmark

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 descriptionn 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 descriptionn n
[testaccountcat] => intl
[deptimages] => Array
(
[0] => [url]http://www.test.com/creativedeptlogo.jpg[/url]

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

[officenumber] => 8531212
)

)

)

Thanks in advance for any assistance

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 04.2012 — Not sure what the exact problem is?
Copy linkTweet thisAlerts:
@JLAauthorJul 05.2012 — Not sure what the exact problem is?[/QUOTE]

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.
Copy linkTweet thisAlerts:
@NogDogJul 05.2012 — 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 [noparse]...[/noparse] 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: [code=php]$out['results'][0]['testAccount'][/code].
Copy linkTweet thisAlerts:
@JLAauthorJul 05.2012 — 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 descriptionn n",
"testaccountcat": "intl",
"deptimages":
[
"http://www.test.com/creativedeptlogo.jpg",
"http://www.test.com/creativedepthead.jpg.jpg"
],
"officenumber": 8531212
}
]
}[/CODE]


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
[code=php]$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);
[/code]


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 descriptionn n
[testaccountcat] => intl
[deptimages] => Array
(
[0] => http://www.test.com/creativedeptlogo.jpg

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

[officenumber] => 8531212
)

)

)[/CODE]


So I saw your comments

They are already in array variables, so there is really no need to assign them to other variables.[/QUOTE]

but for example, if you want to reference the "testingaccount1" value, it would appear to be: [code=php]$out['results'][0]['testAccount'] [/code][/QUOTE]

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 descriptionn 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 descriptionn n

[testaccountcat] => govt
[deptimages] => Array
(
[0] => http://www.test.com/securitydeptlogo.jpg
[1] => http://www.test.com/securitydepthead.jpg

)

[officenumber] => 8765309
)

)

)
[/CODE]


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.....[/CODE]


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.
Copy linkTweet thisAlerts:
@NogDogJul 05.2012 — Sounds like a job for a [url=http://php.net]foreach[/url]() loop.
[code=php]
foreach($out['results'] as $record)
{
echo $record['testAccount'];
echo $record['testnotes']['test_department'];
echo $record['testnotes']['test_office_location'];
}
[/code]

(Obviously you want to do something other than just echo stuff in the loop. ? )
×

Success!

Help @JLA spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...