1

I have a function in which I want to pass to it an array of random numbers from 0 to 3, which would then correlate to then number of JSON objects I have stored. What I ultimately want to do is create an array in which these four JSON objects used to fill a 60 element array, in a random order. My problem so far is that the json_decode is returning null, so I can't proceed any further. I ran json_last_error and it returned 4, which I believe points to a syntax error, but I can't figure it out. Any ideas?

my function:

public function createSpatialKey($random_array){

    $test = array();
    $image_array = file_get_contents('files/memory_key.json');

    $image_array = json_decode($image_array, true);
    print_r($image_array); 

    $type = json_last_error();

    echo $type;

    foreach($random_array as $question){
      array_push($test, $image_array[$question]);
    } 

    return $test;
}

My JSON data:

[
  {
    "image": "<div id='spatial_location1'>1</div>",
    "answer": 1,
    "stimulus_id": 1,
  },
  {
    "image": "<div id='spatial_location2'>2</div>",
    "answer": 2,
    "stimulus_id": 2,
  },
  etc... 
]
4
  • There is an extra comma after the "stimulus_id" entry that renders the JSON invalid. Commented Apr 27, 2015 at 16:10
  • I can't believe I stared at this for an hour, and I completely missed that. Thank you for your sharp eyes! Commented Apr 27, 2015 at 16:15
  • If you put it as an answer, I will check it as so. Commented Apr 27, 2015 at 16:16
  • Great that solved it! I added my answer below, thanks for accepting! Commented Apr 27, 2015 at 16:20

1 Answer 1

1

There is an extra comma after the "stimulus_id" entry that renders the JSON invalid. And I know what you are talking about :-)

Sign up to request clarification or add additional context in comments.

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.