Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Last active March 7, 2018 19:53
Show Gist options
  • Save nhalstead/36b3ed131d3ae7c48d72fc4abcb98adf to your computer and use it in GitHub Desktop.
Save nhalstead/36b3ed131d3ae7c48d72fc4abcb98adf to your computer and use it in GitHub Desktop.
Array Map
<?php
/**
* Check the Array Map against the Input Array.
*
* @author Noah Halstead <[email protected]>
* @link https://gist.github.com/nhalstead/36b3ed131d3ae7c48d72fc4abcb98adf
* @param Array The Map of what the Input should look like.
* @param Array The Input Array.
* @return Array the Steps of the Detection Process.
*/
function expectMap($arrayMap, $select, $recalling = false){
static $fault;
if($recalling == false){
// Clear Fault if not being recalled via it's self.
$fault = array();
}
foreach ($arrayMap as $k => $v){
// If it has to Match Sub Objects.
if( is_array($v) ) {
// Has Sub Objects.
if( isset($select[$k]) && is_array($select[$k]) ){
// Check if the Object Exists and it is an Array.
if( expectMap($arrayMap[$k], $select[$k], true) == true){
// Has Sub Array
$fault['b'][] = true;
continue;
}
else {
// Does not have
$fault['b'][] = false;
break;
}
}
else {
// Does not Have Sub Array
$fault['b'][] = false;
break;
}
}
else {
// Check if the Object Exists and it is an Array.
if( isset($select[$v]) ){
// Has Sub String
$fault['b'][] = true;
continue;
}
else {
// Does not have Sub String
$fault['b'][] = false;
break;
}
}
}
/**
* @link https://stackoverflow.com/a/6850531/5779200
*/
if(count(array_unique($fault['b'])) === 1){
return current($fault['b']);
}
return false;
}
// The Map of what the Data Should look like.
$map = array(
'data' => array(
'meta' => array(
'data',
'mapit',
'two'
)
)
);
// The Data or the Values
$values = array(
'data' => array(
'meta' => array(
'data' => 24,
'mapit' => true,
'two' => 222
)
)
);
header('Content-Type: text/plain');
echo expectMap( $map, $values )?"Matching Map":"Not Maching Map";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment