How to convert ASP Arrays to PHP and viceversa ?
Top Questions
ASP Arrays to PHP
WDDX also allows more-complicated data structures to be passed between applications. Here we will pass an array from an ASP WDDX script to a PHP script.
An ASP Script to Serialize an Array into a WDDX Packet
<%
'define data as ASP array
dim names
names = Array("Andrew", "Emma", "Terry", "Mary", "Thomas")
set wddxob = Server.CreateObject("WDDX.Serializer.1")
'convert ASP array to WDDX array
wddxvar = wddxob.serialize(names)
'output WDDX array
response.write wddxvar
set wddxob = nothing
%>
First, you create the array that you will convert to WDDX:
dim names
names = Array("Andrew", "Emma", "Terry", "Mary", "Thomas")
Note that currently, the WDDX COM object accepts only this definition of an array in ASP. The other definition, shown here, won't work:
'dim names(5)
'names(1) = "Andrew"
Next, you load the WDDX COM object into memory:
set wddxob = Server.CreateObject("WDDX.Serializer.1")
Then you convert the ASP array into WDDX:
wddxvar = wddxob.serialize(names)
Finally, you display the WDDX and unload the WDDX COM object from memory:
response.write wddxvar
set wddxob = nothing
A PHP Script to Deserialize an Array from a WDDX Packet
As with the previous examples, this PHP script obtains the WDDX packet from the ASP script and converts it into a native variable type. However, this time the PHP variable type you must obtain from the WDDX packet is an array.
<?php
//get WDDX array
$wddxdata = join ('', file
('http://localhost/phpbook/Chapter5_Sessions/WDDX/ASP/asparrays_server.
asp'))
;
//put WDDX array into PHP array
$wddxvar = wddx_deserialize("$wddxdata");
//iterate through PHP array
for ($arraycount=0; $arraycount<5; $arraycount++) {
print("$wddxvar[$arraycount]\n\r<BR>");
}
?>
As in the previous example, you use the PHP join function to obtain the
WDDX packet from the ASP script:
$wddxdata = join ('', file
('http://localhost/phpbook/Chapter5_Sessions/WDDX/ASP/asparrays_server.asp'))
;
You then deserialize the WDDX packet into a PHP array:
$wddxvar = wddx_deserialize("$wddxdata");
To display the contents of the array, you loop through and display each item in the array:
for ($arraycount=0; $arraycount<5; $arraycount++) {
print("$wddxvar[$arraycount]\n\r<BR>");
}
Post new comment