Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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"
}]
share|improve this question
    
how are you opening the file, show that part.... – KyleK Jun 21 '13 at 1:00
    
"This doesn't work" -- well, what did you expect to happen, and what actually happened? Please provide enough info to actually answer the question. – Hamish Jun 21 '13 at 1:01
1  
also....if its php in the first place, doesn't that mean it was an array at one point in the cycle....surely before it gets encoded to json, you can upload? no? Am I missing something. Why have another file that decodes it and uploads? – KyleK Jun 21 '13 at 1:01
    
Also, is your question "how do I turn it into an array" (the title) or "how do I put it into mysql" (in the question)? – Hamish Jun 21 '13 at 1:02
up vote 3 down vote accepted

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;
}
share|improve this answer
1  
Thanks that worked perfectly :) – Jonatan Ortheden Jun 21 '13 at 1:36

How about this:

$json = json_decode($string, true);

This should make $json an associative array.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.