LoginSignup
2
0

More than 3 years have passed since last update.

PHPで、ネストの深さがわからない配列中に特定のキーが含まれているかを調べる

Posted at

意外とドンピシャのコードが見つからなかったので、備忘録として書いておきます。

function hasKey($array, $key)
{
    $hasKey = false;
    array_walk_recursive($array, function ($eachItem, $eachKey) use(&$hasKey, $key) {
        $hasKey |= $eachKey === $key;
    });
    return (bool)$hasKey;
}

array_reduce() が使えるのかと思ったんですが、ネストの下層まで潜ってreduceしてくれるわけではないようでした。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0