0

I need to convert tempArr, an associative array, to JSON and write it in a file.

I am using the following code, but I get a json_encode(): recursion detected warning.

$tempArr = array('one' => $var1, 'two'=> $var2, 'three'=>$var3);
$fp = fopen('myFile.json', 'w');
fwrite($fp, json_encode($tempArr));
fclose($fp);

What am I doing wrong?

2
  • Your code works fine for me. I think the variable values are relevant here. Commented Sep 13, 2013 at 12:34
  • I'd begin by looking what is in $var1, $var2, and $var3. Try replacing one a time until you know which one is the problem, then print_r the contents of that variable and post here Commented Sep 13, 2013 at 12:35

1 Answer 1

0

Your error probably boils down to:

$a = array(&$a);
json_encode($a);

See: http://codepad.org/8dYy8Y3C

so you have a reference cycle somewhere in $var1, $var2, or $var3. Your mission: search and destroy it! As Jamie Bicknell commented: remove one at a time to see which one.

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.