Skip to content

Instantly share code, notes, and snippets.

@bystefu
Last active January 4, 2021 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bystefu/51d87f8f78961dbb8b2f72325ec372d0 to your computer and use it in GitHub Desktop.
Save bystefu/51d87f8f78961dbb8b2f72325ec372d0 to your computer and use it in GitHub Desktop.
A little game with PHP MULTYARRAY
<?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