PHP
  Home arrow PHP arrow Page 5 - Data Exchange with XML, WDDX and PHP
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Data Exchange with XML, WDDX and PHP
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2001-09-13

    Table of Contents:
  • Data Exchange with XML, WDDX and PHP
  • The Wonderful World Of WDDX
  • Polly Wants A Cracker
  • Humbert Redfinch-Northbottom The Third, I Presume?
  • Old Friends And New
  • Hip To Be Square
  • The Truth Is Out There
  • Money Talks
  • Closing Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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');// serializeecho 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"));// serializeecho 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 Arrayecho $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'];?>

    More PHP Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT