PHP WDDX: Complex Serialization


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>


Series this article:
PHP WDDX: Introduction
PHP WDDX: Understanding Anatomy of WDDX
PHP WDDX: Understanding Data Elements
PHP WDDX: Understanding Simple Data Type
PHP WDDX: Using Array Elements
PHP WDDX: Struct Element
PHP WDDX: Recordset Element
PHP WDDX: Simple Serialization Using PHP function
PHP WDDX: Complex Serialization
PHP WDDX: Unserializing Data
PHP WDDX: Creating Web Service Server
PHP WDDX: Creating Web Service Client

Bookmark and Share Tag: wddx. distributed data, serialization, complex serialization Category: PHP Application Post : April 30th 2008 Read: 20,906

advertisements


blog comments powered by Disqus