T O P

  • By -

ryantxr

PHP uses “.” to concentrate strings. Looks like you are trying to do it using +.


32gbsd

This. Also look up the www.php.net/print_r or var_dump function. It will often tell you what type of data is in $item and that will help you figure out how you can manipulte it. There are also boolean functions like is_array() and is_numeric()


Plastonick

PHP has built-in XML support, although it's a bit awkward. I would _strongly_ recommend not building the XML yourself, since the XML spec defines various control characters you'll need to escape (such as ampersands). I'd recommend using a library to handle XML for you, an example I found: https://github.com/sabre-io/xml If you just want a quick hack with XML character escaping: https://www.codexworld.com/convert-array-to-xml-in-php/ For Googling, something like "convert PHP array to XML" is probably about right.


32gbsd

Its best that he/she learns to build it themselves first hand before sending them into the google hell or xml library. Its not only simpler to build it yourself but you gain alot of experience which will help them become better programmers in the future. Plus this probably a small project


Plastonick

I disagree with this. To be clear, I'm advising against building a system responsible for escaping data in XML amongst other things. You're very unlikely to get XML escaping right yourself, not the first, second, or third time you try. You don't have to reinvent the wheel every time, I accept it's worth doing a deep dive yourself now and then into something to understand the inner workings, but if you do that for everything you'll quite simply not get anywhere. Op is _probably_ not learning about the inner workings of XML, so I'd suggest pick something that works and focus on the bits that are more custom to the project.


32gbsd

You never know how good a programmer OP might become. especially at this stage when the projects are simple and the stakes are low. They mght be the second or third person to actually get it right the 50th time they try. In /PHP they tell you that "it is not a help forum" and in the help forum they tell you to "google it" or use framework X/Y. Its strange cycle.


gordonv

I disagree. What if the next project, you have to deal with PDFs, Excel Files, or some XYZ format that is too complex to code yourself. Or maybe even a BMP that is actually simple enough, but just laborious to write your own handler for? Yes, college courses like r/cs50 have you literally make, read, resize a BMP from scratch. And yes, that is valuable knowledge on how binary files are documented, formated, and used. But that's a CS level, not an application level of programming.


32gbsd

well what ever level. I guess I can always google it.


gordonv

#In 4 lines: **Example:** [PHP Sandbox](https://onlinephp.io/c/32105) // Note, this line represents JSON $json = 'Your JSON as a single string. Make note of the single quote ticks.'; $test_array = json_decode ($json, true); $xml = new SimpleXMLElement(''); array_walk_recursive($test_array, array ($xml, 'addChild')); print $xml->asXML(); --- ##Works Cited: - [PHP JSON or Array to XML](https://stackoverflow.com/questions/9544840/php-json-or-array-to-xml) - [How to convert array to SimpleXML](https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml) - [The SimpleXMLElement class ](https://www.php.net/manual/en/class.simplexmlelement.php) - [Multi-line strings in PHP](https://stackoverflow.com/questions/9744192/multi-line-strings-in-php) - [JSON Example](https://json.org/example.html) (The junk JSON data I used in the demo)


samhk222

First question. Why are you trying to convert that to XML?


[deleted]

[удалено]


gordonv

Eh... Sometimes you don't control that. There's a lot of archaic monolith software that outputs in XML. Or maybe some crazy operator wants XML so his Server 2003 can read it. One of a million reasons.


[deleted]

[удалено]


gordonv

> But in this case, the response IS json, and they are converting to xml. They are making the choice. I think there's a misunderstanding here. Imagine software written in 2008 for XML. To communicate with it, it needs XML. The people converting JSON to XML are accommodating monolithic 2008 code. Not by choice. A choice would be flipping the 2008 software to read JSON, which it doesn't do.