Bug #71996 [Opn]: Using references in arrays doesn't work like expected

From: Date: Fri, 08 Apr 2016 21:29:26 +0000
Subject: Bug #71996 [Opn]: Using references in arrays doesn't work like expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200446@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71996&edit=1 ID: 71996 User updated by: frankdeweger at gmail dot com Reported by: frankdeweger at gmail dot com Summary: Using references in arrays doesn't work like expected Status: Open Type: Bug Package: SOAP related Operating System: CentOS PHP Version: 7.0.5 Block user comment: N Private report: N New Comment: I made small error in my test script, the correct code is: public function getSoapData() { $soapData = array(); $this->buildElement($soapData, array('element1', 'element2', 'element3', 'element4', 'foo')); $soapData['element1']['element2']['element3']['element4']['foo'] = 'bar'; return $soapData; } Previous Comments: ------------------------------------------------------------------------ [2016-04-08 20:38:21] frankdeweger at gmail dot com Description: ------------ On of our suppliers expects heavily nested XML in the request we make. Constructions like the following are not uncommon in the requests we have to make to them: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Body> <element1> <element2> <element3> <element4> <foo>bar</foo> </element4> </element3> </element2> </element1> </SOAP-ENV:Body> In order to create these nested structures, we used a helper as mentioned in the test script. In PHP 5.4.45 it produced the XML as stated above. However, under PHP 7.0.5, the following XML is generated: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Body> <element1/> </SOAP-ENV:Body> For other people affected by this bug, if you replace the buildElement function with the following code, the generated XML is as expected. private function buildElement(array &$addTo, array $elements) { if (0 === count($elements)) { return; } $element = array_shift($elements); $addTo[$element] = array(); $this->buildElement($addTo, $elements); return; } Test script: --------------- <?php class Example { private function buildElement(array &$addTo, array $elements) { $previous = & $addTo; foreach ($elements as $element) { $previous[$element] = array(); $previous = & $previous[$element]; } } public function getSoapData() { $soapData = array(); $this->buildElement($soapData, array('element1', 'element2', 'element3', 'element4', 'foo')); $soapData['element1']['element2']['element3']['element4']['element4']['foo'] = 'bar'; return $soapData; } } $client = new SoapClient("some.wsdl"); $data = (new Example())->getSoapData(); $client->doSomething($data); Expected result: ---------------- The XML sent to our supplier looks like this: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Body> <element1> <element2> <element3> <element4> <foo>bar</foo> </element4> </element3> </element2> </element1> </SOAP-ENV:Body> Actual result: -------------- The XML sent to our supplier looks like this: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Body> <element1/> </SOAP-ENV:Body> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71996&edit=1

« previous php.bugs (#200446) next »