<?php
$customerProptype = (object)array
(
'first-name' => 'maria',
'last-name' => 'magdalena',
'phone-number' => 'no phone service',
);
$customer = clone $customerProptype;
foreach ($customer as $key => $value) {
$key2 = str_replace('-', '_', $key);
if ($key2 !== $key) {
$customer->$key2 = $customer->$key;
unset($customer->$key);
}
}
var_export($customer, false);
$customer = clone $customerProptype;
foreach ($customer as $key => $value) {
$key2 = str_replace('-', '_', $key);
if ($key2 === $key) {
continue; // nothing to do
}
$customer->$key2 = $customer->$key;
unset($customer->$key);
}
var_export($customer, false);
$customer = clone $customerProptype;
foreach (clone $customer as $key => $value) {
$key2 = str_replace('-', '_', $key);
unset($customer->$key);
$customer->$key2 = $value;
}
var_export($customer, false);
- Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- (object) array(
'first_name' => 'maria',
'last_name' => 'magdalena',
'phone_number' => 'no phone service',
)(object) array(
'first_name' => 'maria',
'last_name' => 'magdalena',
'phone_number' => 'no phone service',
)(object) array(
'first_name' => 'maria',
'last_name' => 'magdalena',
'phone_number' => 'no phone service',
)
preferences:
109.56 ms | 403 KiB | 123 Q