Saypontigohe Saypontigohe - 25 days ago 8
PHP Question

Check if value is in array PHP (easy?)

I think I have a very easy question, but I am stuck anyway. I want to check if the value is in an array, and if it is, i want to change the variable value.

$admin_is_menu = "about";
$test = array();
$test = [
["Name" => "About","alias" => "about"],
["Name" => "Test", "alias" => "test"],
];
if(in_array($admin_is_menu, $test)){
$admin_is_menu = "true";
}
echo $admin_is_menu;


In the code above, it should output the echo
"true"
, since
"about"
is in the array. But is unfortunally does not work.

What am I doing wrong?

Answer

Try array_column to get all array value.

$admin_is_menu = "about";
$test = array();
$test = [
["Name" => "About","alias" => "about"],
["Name" => "Test", "alias" => "test"],
];
if(in_array($admin_is_menu, array_column($test,'alias'))){
    $admin_is_menu = "true";
}
echo $admin_is_menu;

DEMO

Comments