Veridis Posted April 16, 2008 Share Posted April 16, 2008 Hello Everyone, I was wondering if you know anything about how to turn a recursive php array into a recursive javascript array, I've tried json_encode but it turns the references into null values and I need to keep the references. Examples Original php array: $test = array( 'prop1' => array( 'prop1a' => $test['prop2'], 'prop1b' => 'string1b' ), 'prop2' => array( 'prop2a' => $test['prop1'], 'prop2b' => 'string2b' ) ); array/object created by json_encode: test = { "prop1":{ "prop1a":null, "prop1b":"string1b" }, "prop2":{ "prop2a":null, "prop2b":"string2b" } }; What I need: test = { "prop1":{ "prop1a": test.prop2, "prop1b": 'string1b' }, "prop2":{ "prop2a": test.prop1, "prop2b": 'string2b' } } PS: This question has posted on another forum, idk if there's any internet etiquette about duplication of questions, is there? Link to comment https://forums.phpfreaks.com/topic/101327-recursive-php-array-to-javascript-arrayobject/ Share on other sites More sharing options...
Cep Posted April 16, 2008 Share Posted April 16, 2008 I don't think you can do this, $test = array( 'prop1' => array( 'prop1a' => $test['prop2'], 'prop1b' => 'string1b' ), 'prop2' => array( 'prop2a' => $test['prop1'], 'prop2b' => 'string2b' ) ); At this point you are assigning $test, so how can $test['prop2'] be anything but null until, $test is finished being assigned? I may be wrong but I have never seen this done before. Link to comment https://forums.phpfreaks.com/topic/101327-recursive-php-array-to-javascript-arrayobject/#findComment-518332 Share on other sites More sharing options...
Veridis Posted April 16, 2008 Author Share Posted April 16, 2008 Your right, I was writing the array creation code from memory so got a few things wrong (forgot the &s as well). The following should work to create a recursive array: $test = array( 'prop1' => array( 'prop1a' => '', 'prop1b' => 'string1b' ), 'prop2' => array( 'prop2a' => '', 'prop2b' => 'string2b' ) ); $test['prop1']['prop1a'] = &$test['prop2']; $test['prop2']['prop2a'] = &$test['prop1']; Link to comment https://forums.phpfreaks.com/topic/101327-recursive-php-array-to-javascript-arrayobject/#findComment-518357 Share on other sites More sharing options...
Veridis Posted April 16, 2008 Author Share Posted April 16, 2008 bump Link to comment https://forums.phpfreaks.com/topic/101327-recursive-php-array-to-javascript-arrayobject/#findComment-518515 Share on other sites More sharing options...
Cep Posted April 16, 2008 Share Posted April 16, 2008 I thought this was solved? Link to comment https://forums.phpfreaks.com/topic/101327-recursive-php-array-to-javascript-arrayobject/#findComment-518728 Share on other sites More sharing options...
Veridis Posted April 16, 2008 Author Share Posted April 16, 2008 No, I made a mistake in the code for my first question. I still need to find a way to translate the array to javascript although now the array is really a recursive array rather than before where it was broken and not recursive. Link to comment https://forums.phpfreaks.com/topic/101327-recursive-php-array-to-javascript-arrayobject/#findComment-518739 Share on other sites More sharing options...
kenrbnsn Posted April 16, 2008 Share Posted April 16, 2008 I took your second example and when I used json_encode() on it, I got the following warnings: Warning: json_encode(): recursion detected in test_rec.php - on line 16 Warning: json_encode(): recursion detected in test_rec.php - on line 16 I don't think you can do this automatically. How are you thinking of using this array? Ken Link to comment https://forums.phpfreaks.com/topic/101327-recursive-php-array-to-javascript-arrayobject/#findComment-518756 Share on other sites More sharing options...
Veridis Posted April 16, 2008 Author Share Posted April 16, 2008 My PHP array is an array of item objects which hold data about items, the data includes arrays of links to other items (the items which an item can be made into and the items which the item is made out of) and the links are held as references. I could probably rewrite the script to hold the item ID instead of a reference to the item object, but it'd be a lot more fun to use references and I feel that it should be possible to transfer an array between languages with references intact. I think the reason the json encoder doesn't work is that it encodes to an anonymous object and therefore can't reference itself. The 'this' keyword might work if it was parsed twice on the javascript end or if it used the current array name and assumed the arrays would be called the same. Link to comment https://forums.phpfreaks.com/topic/101327-recursive-php-array-to-javascript-arrayobject/#findComment-518815 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.