4

I want to save this json array that my fb-login returns to a database. However I don't really know how to convert that into a string using php.

[{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]

I have tried this code below but I am even not quite sure anymore whether what the fb login return is a json array or not.

$json = '{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}';

var_dump(json_decode($json));

How would I be able to save the different sports to my database in a string?

Any help would be much appreciated.

Thanks.

EDIT: The upper part returns NULL.


SECOND EDIT:

if (array_key_exists('sports', $me)){


                    $json = '$me['sports']'; PROBLEM IS HERE
                    $data = json_decode($json, true);
                    $sports = array();
                    foreach ($data as $item) {
                        $sports[] = $item['name'];
                    }

                    $user->fb_sports = $sports;
                }
4
  • What exactly is the problem? json_encode or serialize should work just fine. Commented Feb 4, 2014 at 9:56
  • $json should have those square brackets around the outside - use jsoneditoronline.org to check if it is valid JSON. Commented Feb 4, 2014 at 9:58
  • And cross your fingers and hope you'll never have to query through this structured data saved as a string. Because who needs normal forms? Commented Feb 4, 2014 at 9:58
  • What is $me equal to? Also $json = '$me['sports']'; is not valid code. Commented Feb 5, 2014 at 0:21

5 Answers 5

7
$json = '[{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]';
$data = json_decode($json, true);
$sports = array();
foreach ($data as $item) {
    $sports[] = $item['name'];
}

if you want commas separating them:

$output = implode(', ', $sports);
Sign up to request clarification or add additional context in comments.

3 Comments

this works fine. However, I get the error that what I receive from Facebook is an array, not a string. I have edited the question.
You need quotes before and after the square brackets like I've said in my answer. Otherwise it isn't valid PHP though it is valid Javascript. BTW what is $me equal to?
Can use use var_dump with $me and copy and paste the results?
1

use :

 $json = serialize($json);

4 Comments

Now this is what is returned: s:134:"{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}"; Is there a way to only save the sports?
$json = '[{"id":"108124505876479","name":"Wakeboarding"}, {"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]'; $json = json_decode($json,true); foreach($json as $value){ $sports[] = $value["name"]; } $sports = serialize($sports);
I have edited the question. I get the error that what I receive from Facebook is an array, not a string.
the Facebook array containing user data from login.
1
$json1 = '{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}';
$json2 = '[{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]';

var_dump(json_decode($json1,true));
var_dump(json_decode($json2,true));

watch the difference

Comments

0

If var_dump(json_decode($json)) returns what you'd expect just save $json directly into the database.

2 Comments

I would like to save a string of all the sports.
there should be square brackets around it all... try copying it into jsoneditoronline.org also use var_dump(json_decode($json, true)) to convert it into associative arrays.
0
$string=mysql_real_escape_string($json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.