0

Let's say I have this:

var achievementData = {
    "achievements": { 
        0: {
            "id": 0,
            "title": "All Around Submitter",
            "description": "Submit and approved one piece of content in all six areas.",
            "xp": 500,
            "level_req": 3
        }, 
        1: {
            "id": 1,
            "title": "Worldwide Photographer",
            "description": "Submit and approved one piece of image content in all six areas.",
            "xp": 1200,
            "level_req": 1
        }, 
        2: {
            "id": 2,
            "title": "Super Submitter",
            "description": "Get approved 10 players and 2 clubs in one week or less.",
            "xp": 2500,
            "level_req": 5
        }, 

        }
};

That's JavaScript, so how I would turn it into PHP? I tried changing brackets and finding information on PHP arrays but it seems it's just SIMPLE examples.

test
  • 17,706
  • 64
  • 171
  • 244
  • 4
    json_decode() http://www.php.net/manual/en/function.json-decode.php –  Sep 16 '12 at 20:45

2 Answers2

4

You should use json_decode() to turn that string into something useful in php, with second parameter set to true it will give you an associative array.

http://php.net/manual/en/function.json-decode.php

Daniel Hallqvist
  • 822
  • 5
  • 15
1

In case you want/need to do that manually:

<?php

$achievementData = new array();

$achievementData[] = new array('id' => 0, 'title' => 'abc', 'description' => 'abc', 'xp' => 500, 'level_req' => 3);

// repeat the same for the other records

?>
Mahdi
  • 9,247
  • 9
  • 53
  • 74
  • How would this be different than JSON decode? Beside it being JSON and PHP Array.. which would be easier to manage? – test Sep 16 '12 at 20:55
  • 1
    It depends. What do you want to do exactly? You want to convert this JS Array to PHP Array for one time? The array is small, big or very big? ... If the array is dynamic, you can give it as an input to the php and then, json_decode works perfectly for you. If you want to just use this array in your php source code, then it's better to write it like above, so it would much more readable and you don't need to provide it as an input in your php file. – Mahdi Sep 16 '12 at 21:01