Skip to content

Instantly share code, notes, and snippets.

@etiennemarais
Last active April 11, 2016 14:03
Show Gist options
  • Save etiennemarais/14ab5d8f1accd815055733da895b833a to your computer and use it in GitHub Desktop.
Save etiennemarais/14ab5d8f1accd815055733da895b833a to your computer and use it in GitHub Desktop.
Recursive match for array keys based of checking through a array of variables in a mustache template with it's includes
<?php
class ArrUtils
{
/**
* @param $searchKey
* @param $dataArray
* @return boolean
*/
protected function isKeyInArray($searchKey, $dataArray)
{
// Ignore endif statements
if ($searchKey[0] === '/') {
return true;
}
// Include if/else blocks
if ($searchKey[0] === '#' || $searchKey[0] === '^') {
$searchKey = substr($searchKey, 2, strlen($searchKey) - 2);
}
if (array_key_exists($searchKey, $dataArray)) {
return true;
}
foreach ($dataArray as $element) {
if (is_array($element)) {
if ($this->isKeyInArray($searchKey, $element)) {
return true;
}
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment