'What will be the alternative of arrayObject()?
During migration of my code from PHP5 to PHP7, I am facing some pretty issues, One of them are arrayObject(). arrayObject in PHP7 is not behaving as expected, what will be the alternate solution to achieve arrayObject functionality.
//I have some data in this carList array
$carList = array();
$carArray = new ArrayObject();
//Go throuch each car record and populate the car object
foreach ($carList as $lst){
$car = new carDetail();
foreach ($lst as $key=>$value){
$car->{lcfirst($key)} = $value;
}
$car = new SoapVar($car, SOAP_ENC_OBJECT, null, null, 'car');
$carArray->append($car);
}
return $carArray;
It results in following Soap Envelope
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://logistics.website.eu">
<SOAP-ENV:Body>
<ns1:OutgoingNoosGoodsOrder>
</carArray>
</ns1:OutgoingNoosGoodsOrder>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Before migration, it is like this on PHP 5
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://logistics.website.eu">
<SOAP-ENV:Body>
<ns1:OutgoingNoosGoodsOrder>
<carArray>
<car>
<make> Hyundai </make>
<model> Verna </model>
<year> 2019 </year>
</car>
<car>
<make> Audi </make>
<model> Q7 </model>
<year> 2022 </year>
</car>
</carArray>
</ns1:OutgoingNoosGoodsOrder>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Solution 1:[1]
I tried running the following code in https://sandbox.onlinephpfunctions.com/ with PHP 7.4.28 and 5.6.40 :
<?php
class carDetail{}
const car = '3';
$carList = [
"something" => [1,2,3],
"another" => [4,5,6],
"yet_another" => [7,8,9]
];
$carArray = new ArrayObject();
//Go throuch each car record and populate the car object
foreach ($carList as $lst){
$car = new carDetail();
foreach ($lst as $key=>$value){
$car->{lcfirst($key)} = $value;
}
$car = new SoapVar($car, SOAP_ENC_OBJECT, null, null, car);
$carArray->append($car);
}
var_dump($carArray);
?>
The results are exactly the same in PHP 7.4.28 and 5.6.40 (screenshot below):

So the problem must be something else, and not how ArrayObject works.
If I were to guess, one thing that stopped working when migrating from PHP5 to PHP7 was:
using an empty string to initialize an array. Like
$myVar = '';
$myVar['key']=[1,2,3];
That doesn't work in PHP7, even though it does in PHP5.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | qrsngky |
