0

I have some variable

$role = 2
$role_id = Array ( [0] => Array ( [role_id] => 2 ) [1] => Array ( [role_id] => 1 ) ) 

for this comparator

 if (in_array($role, $role_id)){
    echo "Match found";
    }
    else
    {
    echo "Match not found";
    }

**result Match not found**

I try with array_search()

 if (array_search($role, $role_id,true)){
    echo "Match found";
    }
    else
    {
    echo "Match not found";
    }

**result Match not found**

how can I get value of role_id ? thanks any help

Nick
  • 138,499
  • 22
  • 57
  • 95
ricky
  • 3
  • 3
  • Array search will search the top level array. If you have a compound array you'll need a deeper search. Take a look at array_column() on php.net – Paul Allsopp Nov 16 '19 at 21:02
  • Does this answer your question? [PHP multidimensional array search by value](https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value) – miken32 Nov 16 '19 at 21:58

1 Answers1

2

$role_id is a two-dimensional array. You need to search in the second, role_id dimension, which you can do using array_column:

$role = 2;
$role_id = array (array('role_id' => 2 ), array('role_id' => 1 ) );
if (in_array($role, array_column($role_id, 'role_id'))) {
    echo "Match found";
}
else {
    echo "Match not found";
}

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95