I have an piece of my array here:
[0] => stdClass Object
(
[id] => 8005
[parentid] => 8004
[name] => a, b
[level] => 1
[prodtypename] => 1, 2, 3, 4
)
[1] => stdClass Object
(
[id] => 8006
[parentid] => 8005
[name] => c
[level] => 2
[prodtypename] =>
)
[2] => stdClass Object
(
[id] => 8007
[parentid] => 8005
[name] => d
[level] => 2
[prodtypename] =>
)
In my form I display them like:
<?php foreach ($this->myArray as $item): ?>
<?php echo $item->prodtypename ?>
it displays me current category and its current type. I need to display parent "prodtypename" next to child type, so we can know, what is parents types for current child category. Im trying to make display look something like this:
[1] => stdClass Object
(
[id] => 8006
[parentid] => 8005
[name] => c
[level] => 2
[prodtypename] => 1,2,3,4
)
Because parents "prodtypename" have 1.2.3.4, so the child inherits it. Thanks.
Based on the array you have already something like this might work assuming that you can rely on parents always appearing in the array before their children:
$prodTypeNames = array();
foreach ($this->myArray as $item) {
$prodTypeName = $item->prodtypename;
if( is_null( $item->prodtypename ) && isset( $prodTypeNames[$item->parentid] ) ) {
$prodTypeName = $prodTypeNames[$item->parentid];
}
$prodTypeNames[$item->id] = $prodTypeName;
echo $prodTypeName;
}
But you're probably better off getting the data in a better format to begin with.