Jump to content

Recursive PHP array to Javascript array/object


Veridis

Recommended Posts

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?

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.

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'];

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

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.