PHP - get all keys from a array that start with a certain string

后端 未结 9 1301
日久生厌
日久生厌 2020-11-30 00:09

I have an array that looks like this:

array(
  \'abc\' => 0,
  \'foo-bcd\' => 1,
  \'foo-def\' => 1,
  \'foo-xyz\' => 0,
  // ...
)
9条回答
  •  抹茶落季
    2020-11-30 00:39

    This is how I would do it, though I can't give you a more efficient advice before understanding what you want to do with the values you get.

    $search = "foo-";
    $search_length = strlen($search);
    foreach ($array as $key => $value) {
        if (substr($key, 0, $search_length) == $search) {
            ...use the $value...
        }
    }
    

提交回复
热议问题