up vote 0 down vote favorite

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

flag

1 Answer

up vote 8 down vote accepted

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

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

link|flag
4  
Or fix the foreach foreach ($userCategories as $key=>&$category) { (note the &) – svens Jun 23 at 21:36
Right; I thought of that but wasn't positive that it would work properly in a foreach. – zebediah49 Jun 23 at 21:37
This worked and putted an end to my misery :) thx!! – Yaniv Jun 23 at 21:51
I thought that using the ampersand was deprecated? but I did a little test, but I didn't get any warnings php.net/manual/en/language.references.pass.php – gawpertron Jun 23 at 22:07

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.