Last active
April 11, 2016 14:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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