0

Here is the code :

<?php 
$a_campagnes = $this->campagne->get_campagnes_client();
foreach($a_campagnes as $o_camp){
    if($o_camp->groupes){
        foreach($o_camp->groupes as $o_groupe){
            if($o_groupe->IDGroupe == $this->session->o_user->IDGroupe){ echo 'ok';}
        }
    }
}
$a_campagnes = array_filter($a_campagnes, function($o_camp){
    if($o_camp->groupes){
        foreach($o_camp->groupes as $o_groupe){
            if($o_groupe->IDGroupe == $this->session->o_user->IDGroupe) return true;
        }
    }
    return false;
});

$a_campagnes contains at first 10 objects

The result of the first foreach is okokokok

The result of $a_campagnes after the array_filter (which is the same code as the first foreach) is null

Where are the four objects matching my first foreach?

EDIT

Just tried that piece of code:

$i_id_groupe_user = $this->session->o_user->IDGroupe;
        foreach($a_campagnes as $o_camp){
            if($o_camp->groupes){
                foreach($o_camp->groupes as $o_groupe){
                    if($o_groupe->IDGroupe == $i_id_groupe_user){ echo 'ok';}
                }
            }
        }
        $a_campagnes = array_filter($a_campagnes, function($o_camp) use ($i_id_groupe_user){
            if($o_camp->groupes){
                foreach($o_camp->groupes as $o_groupe){
                    if($o_groupe->IDGroupe == $i_id_groupe_user) return true;
                }
            }
            return false;
        });

It gives the same result as before

2
  • Please make sure that you are getting $o_camp->groupes in your if condition. If yes then Please check foreach. Commented Mar 7, 2017 at 10:59
  • Condition is good, $o_camp->groupes is an object of objects Commented Mar 7, 2017 at 11:05

1 Answer 1

1

$this doesn't exist inside anonymous functions, and you're trying to use it as if it was inside your class scope, which would be even less logical.

If you want to use whatever $this->session is inside your array_filter() callback, you'll have to either declare a class method specifically for that, or tell the anonymous function that it can use it, like this:

$session = $this->session;
$a_campagnes = array_filter($a_campagnes, function($o_camp) use ($session) {
    if ($o_camp->groupes) {
        foreach($o_camp->groupes as $o_groupe) {
            if ($o_groupe->IDGroupe == $session->o_user->IDGroupe) return true;
        }
    }

    return false;
});
Sign up to request clarification or add additional context in comments.

3 Comments

Just thought about that. Unfortunately, it does not change anything (see my edit)
Well, that's what was wrong with your initial code ... Anything else would be a flaw in your program's logic, which only you know and therefore only you could debug. Also, you would've seen this if you had error_reporting, display_errors enabled ...
Well, you are right... Just found the problem... my variable $a_campagneswas an object of objects... array_filter only accepts arrays as input. Sometime, it's usefull to look at error logs indeed...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.