Hi All,
What is the best way to create an XML stream from an assoc array in php.
Regards Purple
19 2663
Heya, Purple.
You mean something like this: -
array
-
(
-
'name' => 'John',
-
'age' => '38',
-
'orientation' => 'up'
-
);
-
into this: -
<name>John</name>
-
<age>38</age>
-
<orientation>up</orientation>
-
?
Hi pbmods,
yes, thats it - I am working on a generalised function which creates datastreams for extjs. The JSON was simple, the XML doesn't appear so - my assumption is there must be an easier way..
Regards Purple
Heya, Purple.
The fastest way I can think of to accomplish this would be something like this: -
$xml = '<?xml version="1.0" encoding="utf-8"?><introtags ... >';
-
foreach( $data as $_key => $_value )
-
{
-
$xml .= "<{$_key}>{$_value}</{$_key}>";
-
}
-
$xml .= '<closingtags ... >';
-
It's only sightly more complicated if you want to make it pretty to look at :P
Thanks for that pbmods,
after working with the json_encode function:
[PHP]$this->json_data = '{"'.$header.'":'.json_encode($this->queue).'}';[/PHP]
where $this->queue is an assoc array,
I think I was expecting something a little more funky :)
Thanks for your help !
Purple
Heya, Purple.
Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Heya, Purple.
The fastest way I can think of to accomplish this would be something like this: -
$xml = '<?xml version="1.0" encoding="utf-8"?><introtags ... >';
-
foreach( $data as $_key => $_value )
-
{
-
$xml .= "<{$_key}>{$_value}</{$_key}>";
-
}
-
$xml .= '<closingtags ... >';
-
It's only sightly more complicated if you want to make it pretty to look at :P
Hey Pbmods,
I had a question about the syntax you are using within your foreach loop, specifially the "<{$_key}>". I'm curious about the use of brackets. I've seen it before, but I'm not sure that I know what it means. What does using the { }'s do?
Thanks,
Greg
Hey Pbmods,
I had a question about the syntax you are using within your foreach loop, specifially the "<{$_key}>". I'm curious about the use of brackets. I've seen it before, but I'm not sure that I know what it means. What does using the { }'s do?
Thanks,
Greg
It's just another way to put variables inside of a string in php, when there we have double quotes.
It's just another way to put variables inside of a string in php, when there we have double quotes.
But variables (excluding array's) can be placed within a string without the use of { }'s. Does it have something to do with the way the variables are declared in the foreach loop? IE $_key => $_value?
Thanks
Greg
Heya, Greg.
It's really just a style issue for me. When there's curly braces around a variable, it helps make it more obvious to me that there's a variable there. Sometimes, especially when the string is long and distinguished, the '$' gets lost. Adding a curly brace around the variable name just makes it stand out a bit more.
Complex (or "Curly") syntax, as it is called, is also useful if you need to put a variable next to an alphanumeric character in a string: -
$str = $flower . 's are red';
-
$str = "{$flower}s are blue;";
-
Or if you want to use a multidimensional array: -
$str = $ingredients['baking'][$i] . ' is sweet.';
-
$str = "And so are {$pronouns['personal'][$j]}";
-
Or if you need to use variable variables: -
$str = "I lost my {$$obj}; can I have yours?";
-
For more information, check out this document.
PS. How's that article coming?
But variables (excluding array's) can be placed within a string without the use of { }'s.
excluding arrays?
Does it have something to do with the way the variables are declared in the foreach loop? IE $_key => $_value?
- <?Php
-
$myArray = array('ABC','DEF','GHI');
-
-
echo "{$myArray[0]}<br>";
-
echo "$myArray[0]<br>";
-
-
foreach($myArray as $key => $value){
-
echo "$key >> $value<br>";
-
}
-
foreach($myArray as $key => $value){
-
echo "{$key} >> {$value}<br>";
-
}
-
?>
So now what? Its only another way only. but there might be some..(need to read the doc)
Sorry about your thread Purple. It's greg and pb. They gonna hijack this.
Yeah, sorry to hijack the post. I'll make sure it doesn't happen again. I appreciate the info on the curly syntax. In all my books and tutorials I've ever read, I haven't come across the curly syntax often.
Again, thanks for the info.
Greg
Sorry about your thread Purple. It's greg and pb. They gonna hijack this.
Hmph. [WHINE]Greg started it![/WHINE] >P
Hmph. [WHINE]Greg started it![/WHINE] >P
[PHP]<?
for($i=0; $i<10000;$i++){
echo "it's not my fault! It was all Pbmods's fault, having interesting content to his posts!<br />";
}
?>[/PHP]
Greg said emphatically...i'm such a nerd.
[PHP]<?
for($i=0; $i<10000;$i++){
echo "it's not my fault! It was all Pbmods's fault, having interesting content to his posts!<br />";
}
?>[/PHP]
Greg said emphatically...i'm such a nerd.
Very good. You used echo instead of print. But ++$i would give you slightly faster performance ~_^
Very good. You used echo instead of print. But ++$i would give you slightly faster performance ~_^
Hows this:
[PHP]<?
$sql="SELECT * FROM `what_pbmods_knows_tbl`;"
$result=@msql_query($sql);
//loop thru what pbmods knows
while(list($everything)=@mysql_fetch_row($result)) {
//this is a trick question....this loop will run forever!
}
?>[/PHP]
Hows this:
[PHP]<?
$sql="SELECT * FROM `what_pbmods_knows_tbl`;"
$result=@msql_query($sql);
//loop thru what pbmods knows
while(list($everything)=@mysql_fetch_row($result)) {
//this is a trick question....this loop will run forever!
}
?>[/PHP]
Well done, Greg.
However...
The query passed to mysql_query() [or msql_query(), if you prefer 0:) ] should not end with a semicolon ( no, really).
Don't make me split this into its own thread :P
Well done, Greg.
However...
The query passed to mysql_query() [or msql_query(), if you prefer 0:) ] should not end with a semicolon (no, really).
Don't make me split this into its own thread :P
Whoops, besides my typo....no semicolon?! I've never heard that before....are you pulling my leg?
Whoops, besides my typo....no semicolon?! I've never heard that before....are you pulling my leg?
No, really. Check it out:
Parameters
query
A SQL query
The query string should not end with a semicolon.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Joe |
last post by:
Anyone can suggest the best method of reading XML and adding data to
ListView?
Here is the xml data structure::
<xml>
<site>
<url>http://www.yahoo.com</url>
<lastupdate></lastupdate>...
|
by: sherifffruitfly |
last post by:
Hi,
I'm just learning cpp, and the exercise I'm working on is basically as
follows:
1) Create a struct type with 4 members (char, char, char, int).
2) Create an array of, say 3 instances of...
|
by: gs |
last post by:
let say I have to deal with various date format and I am give format string
from one of the following
dd/mm/yyyy
mm/dd/yyyy
dd/mmm/yyyy
mmm/dd/yyyy
dd/mm/yy
mm/dd/yy
dd/mmm/yy
mmm/dd/yy
|
by: KDawg44 |
last post by:
Hi,
Is there a way to get a multidimensional associative array with the
entire result set? I would like to get a an array like this:
resultsArray
How can I accomplish this? Can I do...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |