Web Distributed Data Exchange Step By Step tutorial - Part 9: Don't worry about that title, this code is not complex. even, we want to simply serialization complex data. With PHP's function, we will see simple way. We can handle complex data with three functions: wddx_packet_start(), wddx_add_vars(), and wddx_packet_end().
<?php
// example data
$arrBooks = array(array('id'=>1,'title'=>'PHP Undercover'),
array('id'=>2,'title'=>'PHP Webservices'),
array('id'=>3,'title'=>'Zend Framework')
);
// add header
$wddxst = wddx_packet_start('We build a start');
// add some variables
$rowCount = count($arrBooks);
$fieldCount = 2;
wddx_add_vars($wddxst,'rowCount','fieldCount');
// add complex variable
$i = 0;
for($i=0;$i<$rowCount;$i++){
$varname = 'book'.$i;
$$varname = $arrBooks[$i];
wddx_add_vars($wddxst, $varname);
}
print wddx_packet_end($wddxst);
?>
The result is like this:
<wddxPacket version='1.0'>
<header>
<comment>We build a start</comment>
</header>
<data>
<struct>
<var name='rowCount'>
<number>3</number>
</var>
<var name='fieldCount'>
<number>2</number>
</var>
<var name='book0'>
<struct>
<var name='id'>
<number>1</number>
</var>
<var name='title'>
<string>PHP Undercover</string>
</var>
</struct>
</var>
<var name='book1'>
<struct>
<var name='id'>
<number>2</number>
</var>
<var name='title'>
<string>PHP Webservices</string>
</var>
</struct>
</var>
<var name='book2'>
<struct>
<var name='id'>
<number>3</number>
</var>
<var name='title'>
<string>Zend Framework</string>
</var>
</struct>
</var>
</struct>
</data>
</wddxPacket>