Last active
January 4, 2021 18:20
-
-
Save bystefu/51d87f8f78961dbb8b2f72325ec372d0 to your computer and use it in GitHub Desktop.
A little game with PHP MULTYARRAY
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 | |
function enqueue_css() | |
{ | |
$default = $array = array( | |
array( | |
'name' => 'Bootstrap', | |
'id' => 'bootstrap-css', | |
'url' => 'https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css', | |
'version' => 5, | |
'parent' => NULL, | |
'position' => 0, | |
), | |
array( | |
'name' => 'Toolbar', | |
'id' => 'toolbar-css', | |
'url' => 'toolbar.css', | |
'version' => 5, | |
'parent' => NULL, | |
'position' => 1, | |
), | |
[ | |
'name' => 'Other Primary', | |
'id' => 'other-css', | |
'url' => 'other.css', | |
'version' => 5, | |
'parent' => NULL, | |
'position' => 2, | |
], | |
[ | |
'name' => 'Buttons', | |
'id' => 'buttons', | |
'url' => NULL, | |
'version' => NULL, | |
'parent' => 'bootstrap-css', | |
'position' => 0, | |
], | |
[ | |
'name' => 'Smile', | |
'id' => 'smile', | |
'url' => NULL, | |
'version' => NULL, | |
'parent' => 'other-css', | |
'position' => 1, | |
] | |
); | |
echo "First, parents"; | |
print_r(sortCss($array, 'position', null, 'parent')); | |
echo "<br/>--<br /><br />"; | |
echo "Second,childrens <br />"; | |
print_r(sortCss($array, 'position', 'parent', null)); | |
echo "<br />"; | |
echo "<br />"; | |
echo "<br />"; | |
} | |
function sortCss($array, $value = null, $IncludeValue = null, $ExcludeValue = null) | |
{ | |
/* | |
* check if is array | |
*/ | |
if (!is_array($array)) { | |
return "Var send is not array"; | |
} | |
/* | |
* check if key really exist in array | |
*/ | |
if (!array_key_exists($value, $array[0])) { | |
return "Var not exist!!"; | |
} | |
/* | |
* try to exclude from array | |
*/ | |
if ($ExcludeValue !== null && array_key_exists($ExcludeValue, $array[0])) { | |
foreach ($array as $key => $value) { | |
if ($value[$ExcludeValue] !== NULL || strlen($value[$ExcludeValue])>0) { | |
unset($array[$key]); | |
} | |
} | |
} | |
/* | |
* try to include in array JUST WHO have this value = $includeValue | |
*/ | |
if ($IncludeValue !== null && (array_key_exists($IncludeValue, $array[0]))) { | |
foreach ($array as $key => $value) { | |
if ($value[$IncludeValue] == NULL) { | |
unset($array[$key]); | |
} | |
} | |
} | |
/* | |
* finally, we try to sort | |
*/ | |
usort($array, function ($a, $b) { | |
if ($a['position'] === $b['position']) { | |
return 0; | |
} | |
return ($a['position'] < $b['position']) ? 1 : -1; | |
}); | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment