0

I have a list of categories for my videos, and with this simple foreach loop, I'm displaying all of them inside hyperlinks.

Now, what I want to do is display only some of them, based on the category ID.

Code that display all the categories:

$idx = 0;

foreach ($this->categories as $category) 
{
    echo "\n" . ($idx++ ? '| ' : '') . '<a href="' . KM_Helpers::getCategoryURL($category) . '">' . $category['name'] . '</a>';
}

Let's say now I want to display only the categories with the ids: 2,8,21,22. I can use $category['id'] to get the IDS.

I was thinking of having an array that contains only the ID I want to show...

$myarray = array(2, 8, 21, 22);

My question is, how can I loop in my $category array, displaying only the ID contained in the array? (Based on $category['id'] )

3
  • Won't $idx++ always return true? Commented Apr 26, 2012 at 14:20
  • @Jleagle no. the first time it will return false. $idx++ returns the value of $idx before incrementing it. Commented Apr 26, 2012 at 14:22
  • All of the answers above are assuming that you are pulling the ID into the categories query. Are you doing that? @Surreal Dreams, I read the $category['id'] line more of a question not a statement. Commented Apr 26, 2012 at 14:25

4 Answers 4

2

If the id value matches the index in the categories array, you can just loop over the ones you want:

foreach ($myarray as $id)
{
  $category = $this->categories[$id];
  echo "\n".($idx++ ? '| ' : '') .
    '<a href="' . KM_Helpers::getCategoryURL($category) . '">' .
     $category['name'] . '</a>';
}

Otherwise, you have to do a double-loop (possibly hidden by in_array), either to check if an id is valid or to find the category with a given valid id.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a slick way to approach the problem - iterate over the approved ids instead of iterating over the categories - less overhead. OP never mentions doing anything else that requires touching all the categories each time, so +1.
1
$idx = 0;
$myarray = array(2, 8, 21, 22);
foreach($this->categories as $category) 
{
    if (!in_array($category['id'], $myarray)) {
        continue; // skip it if the id isn't in your array of accceptable IDs
    }
    echo "\n".($idx++ ? '| ' : '').'<a href="'.KM_Helpers::getCategoryURL($category).'">'.$category['name'].'</a>';
}

Comments

1

Not a problem. Add in some simple logic to test if the id is on your list of "approved" ids:

$idx = 0;
$myarray = array(2, 8, 21, 22);

foreach($this->categories as $category) 
{
    if(in_array($category['id'], $myarray)
    {
        echo "\n".($idx++ ? '| ' : '').'<a href="'.KM_Helpers::getCategoryURL($category).'">'.$category['name'].'</a>';
    }
}

This tests the $category['id'] in each iteration of the loop and if it's in your array of ids, you echo the link. Otherwise the category item is ignored and the loop moves on.

Comments

1
$ids= array(2, 8, 21, 22);

$idx = 0;
foreach($this->categories as $id => $category){
    if (in_array($category['id'] , $ids)) {
        echo "\n".($idx++ ? '| ' : '').'<a href="'.KM_Helpers::getCategoryURL($category).'">'.$category['name'].'</a>';
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.