Data Exchange with XML, WDDX and PHP - Old Friends And New (
Page 5 of 9 )
You can serialize more than one variable at a time with the wddx_serialize_vars() function, which accepts a list of variable names and creates a single WDDX packet representing the entire set. The code snippet
<?
// variables
$friends = array("Rachel", "Phoebe", "Monica", "Chandler", "Joey", "Ross");
$total = 34238;
$phrase = "The wild blue fox jumped over the indigo submarine";
$error_flag = false;
// serialize
echo wddx_serialize_vars("friends", "total", "phrase", "error_flag");
?>
would generate the WDDX packet
<wddxPacket version='1.0'>
<header/>
<data>
<struct>
<var name='friends'>
<array length='6'>
<string>Rachel</string>
<string>Phoebe</string>
<string>Monica</string>
<string>Chandler</string>
<string>Joey</string>
<string>Ross</string>
</array>
/var>
<var name='total'>
<number>34238</number>
</var>
<var name='phrase'>
<string>The wild blue fox jumped over the indigo submarine</string>
</var>
<var name='error_flag'>
<boolean value='false'/>
</var>
</struct>
</data>
</wddxPacket>
Note that the line breaks have been added by me for clarity - PHP generates the entire packet as a single string.
You can also serialize an associative array
<?
// set up array
$star_wars = array('princess' => 'Leia', 'teacher' => 'Yoda', 'new hope' =>
'Luke', 'bad guy' => 'Darth', 'worse guy' => 'The Emperor');
// serialize
echo wddx_serialize_vars("star_wars");
?>
into this WDDX representation:
<wddxPacket version='1.0'>
<header/>
<data>
<struct>
<var name='star_wars'>
<struct>
<var name='princess'>
<string>Leia</string>
</var>
<var name='teacher'>
<string>Yoda</string>
</var>
<var name='new hope'>
<string>Luke</string>
</var>
<var name='bad guy'>
<string>Darth</string>
</var>
<var name='worse guy'>
<string>The Emperor</string>
</var>
</struct>
</var>
</struct>
</data>
</wddxPacket>
Wanna really cause some heartburn? Try serializing an array of arrays.
<?
// array of arrays
$all_mixed_up = array(
array("red", "green", "blue"),
array("laurel", "hardy"),
array("macaroni", "spaghetti", "lasagne", "fettucine"),
array("Spiderman", "Superman", "Human Torch", "Batman"),
array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke",
"bad guy" => "Darth", "worse guy" => "The Emperor")
);
// serialize
echo wddx_serialize_vars("all_mixed_up");
?>
Here's the WDDX representation:
<wddxPacket version='1.0'>
<header/>
<data>
<struct>
<var name='all_mixed_up'>
<array length='5'>
<array length='3'>
<string>red</string>
<string>green</string>
<string>blue</string>
</array>
<array length='2'>
<string>laurel</string>
<string>hardy</string>
</array>
<array length='4'>
<string>macaroni</string>
<string>spaghetti</string>
<string>lasagne</string>
<string>fettucine</string>
</array>
<array length='4'>
<string>Spiderman</string>
<string>Superman</string>
<string>Human Torch</string>
<string>Batman</string>
</array>
<struct>
<var name='princess'>
<string>Leia</string>
</var>
<var name='teacher'>
<string>Yoda</string>
</var>
<var name='new hope'>
<string>Luke</string>
</var>
<var name='bad guy'>
<string>Darth</string>
</var>
<var name='worse guy'>
<string>The Emperor</string>
</var>
</struct>
</array>
</var>
</struct>
</data>
</wddxPacket>
Note that when this structure is deserialized, it will result in an associative array containing the single key "star_wars", which points to an array of the original values. The following example demonstrates this:
<?
// array of arrays
$all_mixed_up = array(
array("red", "green", "blue"),
array("laurel", "hardy"),
array("macaroni", "spaghetti", "lasagne", "fettucine"),
array("Spiderman", "Superman", "Human Torch", "Batman"),
array("princess" => "Leia", "teacher" => "Yoda", "new hope" => "Luke",
"bad guy" => "Darth", "worse guy" => "The Emperor")
);
// serialize
$packet = wddx_serialize_vars("all_mixed_up");
// and deserialize (as associative array)
$structure = wddx_deserialize($packet);
// returns Array
echo $structure;
// retrieve keys
$keys = array_keys($structure);
// returns "all_mixed_up"
echo $keys[0];
// returns Array (first element of original $all_mixed_up)
echo $structure['all_mixed_up'][0];
// returns "red"
echo $structure['all_mixed_up'][0][0];
// returns "Yoda"
echo $structure['all_mixed_up'][4]['teacher'];
?>