Home arrow PHP arrow Page 5 - Data Exchange with XML, WDDX and PHP

Old Friends And New - PHP

You may never have heard of it before - but if your job involvesexchanging data between different servers and applications, you're going tobe hearing a lot about it very soon. Find out how WDDX makes it possible tocreate and transmit platform-neutral data structures across the Web, andhow it can be combined with PHP to create a whole new generation of Webapplications (including new content syndication and financial updateservices).

TABLE OF CONTENTS:
  1. Data Exchange with XML, WDDX and PHP
  2. The Wonderful World Of WDDX
  3. Polly Wants A Cracker
  4. Humbert Redfinch-Northbottom The Third, I Presume?
  5. Old Friends And New
  6. Hip To Be Square
  7. The Truth Is Out There
  8. Money Talks
  9. Closing Time
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
September 13, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: