i'm trying to build an array from an Object in PHP. I only want certain properties from the object but I don;t know what they will be each time. The names of the properties I need are stored in an array. Here is how my code works currently:
// Hard-coded attributes 'colour' and 'size'
while ($objVariants->next())
{
$arrVariants[] = array
(
'pid' => $objVariants->pid,
'size' => $objVariants->size,
'colour' => $objVariants->colour,
'price' => $objVariants->price
);
}
Instead of hard coding the attributes (colour and size) I want to use variables, this is because it may not always be colour and size depending on what the user has set in the CMS. For example:
$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
);
}
The above code doesn't work, but hopefully it illustrates what i'm trying to do. Any help would be appreciated, thank you!