Check selected category

Asked

Viewed 27 times

0

I need to check if the categories chosen by the user exist in the categories suggested by the php system

I was able to save in the array the categories chosen by the user and also categories suggested by the system.

foreach($array_id_categoria_escolhida as $value_as){
   echo $value_as;
}

echo " ";

foreach($array_categoria_sugerida as $value_ass){
   echo $value_ass;
}

How can I join these two foreach and check if the user has selected the categories suggested by the system?

1 answer

0

The logic is more or less this, I have not tested the code but it is likely to work

foreach($array_categoria_sugerida as $value_ass){

    $resul = array_search($value_ass, $array_id_categoria_escolhida);

if ($resul != false){

    $iguais[] = $resul;

  }
}

if(isset($iguais)) {
    foreach($iguais as $valor) {
        $res[] = $array_id_categoria_escolhida[$valor];
    }
}

//todas que batem
var_dump($res);
  • array(2) { [0]=> string(1) "1" [1]=> string(1) "4" } the result was this

  • in the test I did here was to return only one id_category, because in this case only one category is missing from the suggestion list

  • the result of my foreach is thus chosen 214 suggested 1234

  • in my example varri the array 'chosen' passing value by value of the array 'suggested', the values that were equal automatically he chose based on the dirt, and stored them in $res

Browser other questions tagged

You are not signed in. Login or sign up in order to post.