Скоро гледах някъде такава тема, та и мен ме интересува, какво бихте ме посъветвали в следния случай: имам следния масив:
И имам едни обкет, който има абсолютно същата структура:
Нищо особено, НО проблема е, как мога да задам(прехвърля) стойностите на масива, като стойности на съответните свойства, със същите имена! С рефлекшън си вземам името на property-то, и итерирам масива, но имам два пъти city, и как точно да задам, че един път city при address е едно, а city при township е друго!
PHP:
$baseArray = array(
"name" => "Moni",
"family" => "Fischer",
"age" => 25,
"address" => array (
"city" => "Sofiq",
"street" => "Wrajdebna",
"house_number" => 11
),
"township" => array(
"city" => "Varna",
"street" => "Kaisiewa",
"house_number" => 33
)
);
И имам едни обкет, който има абсолютно същата структура:
PHP:
class Info {
private $name;
private $family;
private $age;
private $address;
private $township;
function __construct() {
$this->name = NULL;
$this->family = NULL;
$this->age = NULL;
$this->address = new Address();
$this->township = new Township();
}
}
class Address {
private $city;
private $street;
private $house_number;
function __construct() {
$this->city = NULL;
$this->street = NULL;
$this->house_number = NULL;
}
public function getCity() {
return $this->city;
}
public function setCity($city) {
$this->city = $city;
}
public function getStreet() {
return $this->street;
}
public function setStreet($street) {
$this->street = $street;
}
public function getHouse_number() {
return $this->house_number;
}
public function setHouse_number($house_number) {
$this->house_number = $house_number;
}
}
class Township {
private $city;
private $street;
private $house_number;
function __construct() {
$this->city = NULL;
$this->street = NULL;
$this->house_number = NULL;
}
public function getCity() {
return $this->city;
}
public function setCity($city) {
$this->city = $city;
}
public function getStreet() {
return $this->street;
}
public function setStreet($street) {
$this->street = $street;
}
public function getHouse_number() {
return $this->house_number;
}
public function setHouse_number($house_number) {
$this->house_number = $house_number;
}
}
Нищо особено, НО проблема е, как мога да задам(прехвърля) стойностите на масива, като стойности на съответните свойства, със същите имена! С рефлекшън си вземам името на property-то, и итерирам масива, но имам два пъти city, и как точно да задам, че един път city при address е едно, а city при township е друго!