Reputation: 5013
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
)
Upvotes: 0
Views: 125
Reputation: 1006
See if this works for you,
foreach($this->primary as $key=>$item)
{
$headingPrinted = false;
foreach($getids->categories as $key=>$catID) {
if($catID == $item->categoryid){
if (!$headingPrinted) {
echo '<h2>'.$item->categoryname.'</h2>';
$headingPrinted = true;
}
echo $item->title;
}
}
}
Upvotes: 1
Reputation: 764
Assuming you want to preserve the order of categories as returned by $getids->categories, you could try something like this:
//Group items by category.
$itemsByCategory = array()
foreach($this->primary as $item) {
if ( !isset($itemsByCategory[$item->categoryid] ) {
$itemsByCategory[$item->categoryid] = array();
}
$itemsByCategory[$item->categoryid][] = $item;
}
//Print items in each category.
foreach($getids->categories as $mycategory):
if ( isset($itemsByCategory[$mycategory]) ) {
$categoryName = $itemsByCategory[$mycategory][0]->categoryname;
echo '<h2>', $categoryName ,'</h2>';
foreach($$temsByCategory[$mycategory] as $key=>$item){
echo $item->title;
}
}
endforeach;
Upvotes: 1
Reputation: 7096
With your current setup, you have no option except to call categoryName 25 times. If you want to avoid doing this, I would consider refactoring your code so that $this->primary is not filled with everything. Assuming you are grabbing these values from a model, you could change your model function to restrict by category name.
foreach($categoryNames as $categoryName) {
$this->primaryArray[$categoryName] = Foo.getByCategoryName($categoryName);
}
foreach($this->primaryArray as $categoryName => $primary) {
echo '<h2>' . $categoryName . '</h2>';
foreach($primary as $key=>$item){
echo $item->title;
}
}
EDIT
To sort by categories (though you will still call 25 times):
$primaryArray = array();
// sort array by category
foreach($this->primary as $value) {
$primaryArray[$value->categoryName] = $value;
}
foreach($primaryArray as $categoryName => $primary) {
echo '<h2>' . $categoryName . '</h2>';
foreach($primary as $key=>$item){
echo $item->title;
}
}
Upvotes: 1