[英]In PHP, how can I create and add to an object?
就是这样,您知道我在使用WordPress。 我有一个数组,想要从该数组创建一个仅包含某些值的对象。
然后,我有另一个单独的数组,我想添加到这个新对象中。 我可能使事情复杂化了。 如果是我,请告诉我。
这是我到目前为止的内容:
$custom = get_post_custom(); //Gets array of values
$picObject = (object)$custom; //Creates object
$picCount = $custom['picturecount'][0];
for ($x = 1; $x <= $picCount; $x++) {
// This assembles a URL that I want to add to the array.
$finalUrl = $picUrl.$gsi.'&picfilename='.$vin.'_00'.$x.'.jpg';
}
需要帮助请叫我。 预先感谢大家!
如果要创建一个仅包含数组中某些值的对象,则不应该强制转换该数组,因为最终将获得其所有值。 而是创建一个新对象并设置所需的值:
$array = array(
'foo' => 'bar',
'bar' => 'baz'
);
$object = new stdClass();
$object->bar = $array['bar'];
$object->something_else = 'w00t!';
将数组$object->new_property = 'foo';
转换为对象(即(object)$array
)将获得相同类型的对象,因此仍然可以使用$object->new_property = 'foo';
给它添加东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.