Jonatan Ortheden
Jonatan Ortheden

Reputation: 190

PHP turning json into array

So I've got this huge amount of data in a php-file but it is in json-format.

I have tried converting it putting all the json into one $string variable. Then:

$json = json_decode($string);

foreach($json as $key => $value) {
  echo $value;
}

This doesn't work though so I'm woundering how I can put all this data into a mysql-database instead (or arrays).

This is a small part of the data.

[{
"namn":"ABF VUX",
"schoolID":"85740",
"stad":"G\u00f6teborg",
"PeriodDropDownList":false,
"FreeTextBox":false,
"code":"680378",
"lan":"V\u00e4stra G\u00f6talands l\u00e4n",
"WeekDropDownList":true,
"TypeDropDownList":true,
"startTid":"-"
},
{
"namn":"Adolf Fredriks Musikklasser",
"schoolID":"29320",
"stad":"Stockholm",
"PeriodDropDownList":true,
"FreeTextBox":true,
"code":"",
"lan":"Stockholms l\u00e4n",
"WeekDropDownList":true,
"TypeDropDownList":true,
"startTid":"8:15"
}]

Upvotes: 1

Views: 640

Answers (2)

jeroen
jeroen

Reputation: 91762

It all depends on the exact json, but your example code generates an array of objects so that is why echo does not work.

What should work with your example, is something like:

$json = json_decode($string);

foreach($json as $key => $value) {
  echo $value->namn;
}

Upvotes: 3

jh314
jh314

Reputation: 27812

How about this:

$json = json_decode($string, true);

This should make $json an associative array.

Upvotes: 1

Related Questions