我是php
的新手。我只想从字符串数组中创建一个对象。
我想要这些字符串数组:
$arrEmails = [
'johndoe@email.com',
'foobar@email.com',
'petersmith@email.com'
];
要放入要访问的对象,如,
$emails = $objEmails->email
怎么可能?
答案 0 :(得分:1)
只需创建空对象,只需添加该属性即可:
$objEmails = new stdClass; // initialize an empty object
$objEmails->email = $arrEmails; // declare the property
答案 1 :(得分:1)
你应该有这样一个类:
class ObjEmails
{
$emails = array();
public function __construct()
{
$this->emails = array(
'johndoe@email.com',
'foobar@email.com',
'petersmith@email.com'
);
}
}
$objEmails = new ObjEmails;
$emails = $objEmails->email;
答案 2 :(得分:0)
您也可以尝试使用 -
$arrEmails = [
'johndoe@email.com',
'foobar@email.com',
'petersmith@email.com'
];
$objEmails = (object) $arrEmail;