News:

Build a stunning handcrafted website with IT Acumens

Main Menu

PHP Functions for WDDX

Started by ganeshbala, Jul 10, 2008, 08:13 PM

Previous topic - Next topic

ganeshbala

PHP Functions for WDDX

PHP has a few other functions that can be useful when you're working with WDDX:

<?php

$names 
= array("Andrew""Emma");
$name2 "Terry";
$name3 "Mary";
$name4 "Thomas";
$wddxpack wddx_packet_start("PHP-WDDX");

wddx_add_vars($wddxpack"names");

wddx_add_vars($wddxpack"name2");
wddx_add_vars($wddxpack"name3");
wddx_add_vars($wddxpack"name4");

$wddxvar wddx_packet_end($wddxpack);

//output WDDX data
$wddx_out wddx_deserialize($wddxvar);

//iterate through PHP array

while (list($key$val) = each($wddx_out["names"])) {
print 
"$val\n<BR>";
}

print(
$wddx_out["name2"] . "<br>");
print(
$wddx_out["name3"] . "<br>");
print(
$wddx_out["name4"] . "<br>");

?>

PHP lets you modify the contents of a WDDX packet so that you can add and delete the contents of a WDDX packet as you see fit. To show this, you first create a PHP array containing the first two items in the packet:

$names = array("Andrew", "Emma");
You then create the other items that make up the WDDX packet:

$name2 = "Terry";
$name3 = "Mary";
$name4 = "Thomas";
You then create the WDDX packet:

$wddxpack = wddx_packet_start("PHP-WDDX");
You then add the items to the WDDX packet and close it:

wddx_add_vars($wddxpack, "names");

wddx_add_vars($wddxpack, "name2");
wddx_add_vars($wddxpack, "name3");
wddx_add_vars($wddxpack, "name4");

$wddxvar = wddx_packet_end($wddxpack);
If you look at the contents of the WDDX packet, you can see how PHP has built it:

<struct>
<var name='names'>
<array length='2'>
<string>Andrew</string>
<string>Emma</string>
</array>
</var>
<var name='name2'>
<string>Terry</string>
</var>
<var name='name3'>
<string>Mary</string>
</var>
<var name='name4'>
<string>Thomas</string>
</var>
</struct>
It's interesting to note that PHP adds each data type (the array and other elements) separately to the WDDX packet. Also note that PHP uses named references for each data element added to the WDDX packet, such as names for the array, name2 for "Terry", and so on.

$wddx_out = wddx_deserialize($wddxvar);
You deserialize the WDDX packet as normal. Because there are different data types within the WDDX packet, PHP handles the deserialized data differently. For the array, you use the list function that you used in the previous example:

while (list($key, $val) = each($wddx_out["names"])) {
print "$val\n<BR>";
}
For the other data types, you use the reference contained in the WDDX file:

print($wddx_out["name2"] . "<br>");
print($wddx_out["name3"] . "<br>");
print($wddx_out["name4"] . "<br>");

In effect, what is returned to PHP from the WDDX packet is a hash array (or named array). Each element in the array takes its named value from the reference in the WDDX packet