<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>wddx examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="wddx.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.wddx.html">WDDX Functions</a></div> <div class="up"><a href="wddx.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="wddx.examples-serialize" class="section"> <h2 class="title">wddx examples</h2> <p class="para"> All the functions that serialize variables use the first element of an array to determine whether the array is to be serialized into an array or structure. If the first element has string key, then it is serialized into a structure, otherwise, into an array. </p> <div class="example" id="example-5523"> <p><strong>Example #1 Serializing a single value with WDDX</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">wddx_serialize_value</span><span style="color: #007700">(</span><span style="color: #DD0000">"PHP to WDDX packet example"</span><span style="color: #007700">, </span><span style="color: #DD0000">"PHP packet"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> This example will produce: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> <wddxPacket version='1.0'><header comment='PHP packet'/><data> <string>PHP to WDDX packet example</string></data></wddxPacket> </pre></div> </div> </div> <div class="example" id="example-5524"> <p><strong>Example #2 Using incremental packets with WDDX</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$pi </span><span style="color: #007700">= </span><span style="color: #0000BB">3.1415926</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$packet_id </span><span style="color: #007700">= </span><span style="color: #0000BB">wddx_packet_start</span><span style="color: #007700">(</span><span style="color: #DD0000">"PHP"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">wddx_add_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$packet_id</span><span style="color: #007700">, </span><span style="color: #DD0000">"pi"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Suppose $cities came from database */<br /></span><span style="color: #0000BB">$cities </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"Austin"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Novato"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Seattle"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">wddx_add_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$packet_id</span><span style="color: #007700">, </span><span style="color: #DD0000">"cities"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$packet </span><span style="color: #007700">= </span><span style="color: #0000BB">wddx_packet_end</span><span style="color: #007700">(</span><span style="color: #0000BB">$packet_id</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$packet</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> This example will produce: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> <wddxPacket version='1.0'><header comment='PHP'/><data><struct> <var name='pi'><number>3.1415926</number></var><var name='cities'> <array length='3'><string>Austin</string><string>Novato</string> <string>Seattle</string></array></var></struct></data></wddxPacket> </pre></div> </div> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If you want to serialize non-ASCII characters you have to convert your data to UTF-8 first (see <span class="function"><a href="function.utf8-encode.html" class="function">utf8_encode()</a></span> and <span class="function"><a href="function.iconv.html" class="function">iconv()</a></span>). </p> </p></blockquote> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="wddx.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.wddx.html">WDDX Functions</a></div> <div class="up"><a href="wddx.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>