3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json_1 = '[{"MobileNo":"123"},{"MobileNo":"456"}]'; $json_2 = '[{"MobileNo":"07123456701"}]'; $multiple = json_decode($json_1, true); $single = json_decode($json_2, true); class convert{ public function arrayToXml($array, $xml) { foreach ($array as $key => $value) { if (is_array($value)) { self::arrayToXml($value, $xml->addChild($key)); } else { if (is_numeric($key)) { $xml->addChild(preg_replace('/s$/', '', $xml->getName()), $value); } else { $xml->addChild($key, $value); } } } } } $xml = new \SimpleXMLElement('<Msg />'); $array = [ 'JobId' => 77]; $i = 0; foreach ($multiple as $mobile) { $i++; $array[$i]['SubmitResult'] = [ 'MobileNo' => $mobile['MobileNo'], 'StatusId' => 0000, 'StatusText' =>'Success: Message submitted successfully', ]; } //print_r($array); $convert = new convert(); $convert->arrayToXml($array, $xml); var_dump($xml->asXML());

preferences:
56.76 ms | 402 KiB | 5 Q