I am trying to get specific value from an array that is filled with news items and its parameters.
My array has 25 items, from 6 different categories, I need to get only category name but not 25 times. If I’m not entirely clear, just say so
This is print_r($this->primary):
Array(
[0] => stdClass Object
(
[title]=> Title 1
[categoryname]=> Cat1
[categoryid]=> 2
)
[1] => stdClass Object
(
[title]=> Title 2
[categoryname]=> Cat1
[categoryid]=> 2
)
[2] => stdClass Object
(
[title]=> Title 3
[categoryname]=> Cat2
[categoryid]=> 3
)
[3] => stdClass Object
(
[title]=> Title 4
[categoryname]=> Cat2
[categoryid]=> 3
)
)
My loop is:
for each ($getids->categories as $key=>$mycategory)
{
echo ' <h2> CAT NAME SHOULD GO HERE</h2> ';
for each ($this->primary as $key=>$item){
if ($mycategory == $item->categoryid){
echo $item->title;
}
}
}
The problem is I need the category name before starting the for each. The stupid thing is that $getids->categories is not giving me anything else but category ID. I can’t fetch it with $this->primary[0] because the items are not grouped per category, but are just rolled in to the array as you can see above ,
any help is appreciated. Thank you!
EDIT: This is print_r($getids->categories):
Array
(
[0] => 11
[1] => 14
[2] => 12
[3] => 13
[4] => 15
[5] => 21
[6] => 20
[7] => 29
)
See if this works for you,