php - How to get the key in an array when the value is known and the keys are strings of numbers?

one text

Solution:

You can typecast if needs a string result:


    $arr = [
        "0" => "Zero",
        "1" => "One",
        "2" => "Two",
    ];
    
    $val = (string) array_search("Zero", $arr, true);// You can typecast to String here
    
    echo gettype($val); // this will return string now
    echo $val;

Source