大佬教程收集整理的这篇文章主要介绍了php-从未知对象属性构建数组,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从PHP中的对象构建数组.我只想要对象的某些属性,但我不知道它们每次都是什么.我需要的属性名称存储在数组中.这是我的代码当前的工作方式:
// Hard-coded attributes 'colour' and 'size'
while ($objVariants->next())
{
$arrVariants[] = array
(
'pid' => $objVariants->pid,
'size' => $objVariants->size,
'colour' => $objVariants->colour,
'price' => $objVariants->price
);
}
我不想使用变量来对属性(颜色和大小)进行硬编码,这是因为根据用户在CMS中设置的内容,它不一定总是颜色和大小.例如:
$arrVariantAttr = $this->getVariantAttr(); // Get the names of the custom variants and put them in an array e.g colour, size
while ($objVariants->next())
{
$arrVariants[] = array
(
'pid' => $objVariants->pid,
foreach($arrVariantAttr as $attr)
{
$attr['name'] => $objVariants-> . $attr['name']; // Get each variant out of the object and put into an array
}
'price' => $objVariants->price
);
}
上面的代码不起作用,但希望它能说明我正在尝试做的事情.任何帮助,将不胜感激,谢谢!
解决方法:
尝试这样的事情:
$arrVariants[] = Array(
'pid' => $objVariants->pid,
'price' => $objVariants->price
);
while( $objVariants->next() )
{
foreach( $arrVariantAttr as $attr )
{
end($arrVariants)[$attr['name']] = $objVariants->$attr['name'];
}
}
以上是大佬教程为你收集整理的php-从未知对象属性构建数组全部内容,希望文章能够帮你解决php-从未知对象属性构建数组所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。