views:

48

answers:

1

Hi I'm trying to check if a certain category is allready selected by looping through an array of categories also I want to add another element to the array whci is just a bit to indicate is the category selcated

my categories array looks like this

0=>array(category_id=>12,category_name=>"blogger")  
1=>array(category_id=>13,category_name=>"dancer")

etc...
now the code i'm trying goes like that:

foreach ($userCategories as $key=>$category) {
    if($category['category_id'] == $mediaDetails['currentCategory']) {
        $category['current'] = 1;
    } else {
        $category['current'] = 0;
    }
}

when executing

die(var_dump($userCategories));

I expect to get an array similar to

0=>array(category_id=>12,category_name=>"blogger",current=>0)  
1=>array(category_id=>13,category_name=>"dancer",current=>1)

but instead I get the same array I had before the foreach loop

any ideas?

Thanks

+8  A: 

It looks like $category is not getting passed by reference.

Try $userCategories[$key]['current']=1 instead, and see how that works.

zebediah49
svens
Right; I thought of that but wasn't positive that it would work properly in a foreach.
zebediah49
This worked and putted an end to my misery :) thx!!
Yaniv
I thought that using the ampersand was deprecated? but I did a little test, but I didn't get any warningshttp://php.net/manual/en/language.references.pass.php
gawpertron