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..
The fastest way I can think of to accomplish this would be something like this:
[code=php]
$xml = '<?xml version="1.0" encoding="utf-8"?><introta gs ... >';
foreach( $data as $_key => $_value )
{
$xml .= "<{$_key}>{$_va lue}</{$_key}>";
}
$xml .= '<closingtags ... >';
[/code]
It's only sightly more complicated if you want to make it pretty to look at :P
The fastest way I can think of to accomplish this would be something like this:
[code=php]
$xml = '<?xml version="1.0" encoding="utf-8"?><introta gs ... >';
foreach( $data as $_key => $_value )
{
$xml .= "<{$_key}>{$_va lue}</{$_key}>";
}
$xml .= '<closingtags ... >';
[/code]
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?
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?
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:
[code=php]
$str = $flower . 's are red';
$str = "{$flower}s are blue;";
[/code]
Or if you want to use a multidimensiona l array:
[code=php]
$str = $ingredients['baking'][$i] . ' is sweet.';
$str = "And so are {$pronouns['personal'][$j]}";
[/code]
Or if you need to use variable variables:
[code=php]
$str = "I lost my {$$obj}; can I have yours?";
[/code]
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.
Comment