Сортировка массива зависит от других значений ключей массива

Может ли кто-нибудь показать мне пример того, как сортировать этот массив в зависимости от ключа зависимости для каждого массива. Я хотел бы, чтобы массив был в порядке зависимости, поэтому сначала jquery, затем cookie, bootstrap, checkbox, admin. Я посмотрел на другие сообщения, но они не имели для меня смысла. Это небольшая часть полного массива, массив может быть в любом порядке и длине.

Может ли кто-нибудь показать мне фрагмент кода, который сделает это.

Array ( [0] => Array ( [name] => jquery [version] => 1.1 [file] => vendor/jquery/jquery.js ) [1] => Array ( [name] => cookie [version] => 1.0 [file] => vendor/cookie/cookie.js [dependency] => Array ( [0] => administration [1] => jquery ) ) [2] => Array ( [name] => bootstrap [version] => 1.0 [file] => vendor/bootstrap/js/bootstrap.js [dependency] => Array ( [0] => jquery ) ) [3] => Array ( [name] => checkbox [version] => 1.0 [file] => vendor/checkbox/checkbox.js [dependency] => Array ( [0] => jquery [1] => sticky ) ) [4] => Array ( [name] => datepicker [version] => 1.0 [file] => vendor/datepicker/datepicker.js [dependency] => Array ( [0] => jquery ) ) [5] => Array ( [name] => nanobar [version] => 1.0 [file] => vendor/nanobar/nanobar.js [dependency] => Array ( [0] => jquery ) ) [6] => Array ( [name] => owlcarousel [version] => 1.0 [file] => vendor/owlcarousel/owlcarousel.js [dependency] => Array ( [0] => jquery ) ) [7] => Array ( [name] => selectmultiple [version] => 1.0 [file] => vendor/selectmultiple/selectmultiple.js [dependency] => Array ( [0] => jquery ) ) [8] => Array ( [name] => selectric [version] => 1.0 [file] => vendor/selectric/selectric.js [dependency] => Array ( [0] => jquery ) ) [9] => Array ( [name] => sortable [version] => 1.0 [file] => vendor/sortable/sortable.js [dependency] => Array ( [0] => jquery ) ) [10] => Array ( [name] => uisortableanimation [version] => 1.0 [file] => vendor/uisortableanimation/uisortableanimation.js [dependency] => Array ( [0] => jquery ) ) [11] => Array ( [name] => summernote [version] => 1.0 [file] => vendor/summernote/summernote.js [dependency] => Array ( [0] => jquery ) ) [12] => Array ( [name] => validation [version] => 1.0 [file] => vendor/validation/validation.js [dependency] => Array ( [0] => jquery ) ) [13] => Array ( [name] => sticky [version] => 1.0 [file] => vendor/sticky/sticky.js [dependency] => Array ( [0] => cookie [1] => jquery ) ) [14] => Array ( [name] => jrate [version] => 1.0 [file] => vendor/jrate/jrate.js [dependency] => Array ( [0] => jquery ) ) [15] => Array ( [name] => retina [version] => 1.1 [file] => vendor/retina/retina1.js [dependency] => Array ( [0] => jquery ) ) [16] => Array ( [name] => confirmation [version] => 1.0 [file] => vendor/confirmation/confirmation.js [dependency] => Array ( [0] => jquery ) ) [17] => Array ( [name] => bootstrapfilestyle [version] => 1.0 [file] => vendor/bootstrapfilestyle/bootstrap-filestyle.js [dependency] => Array ( [0] => jquery ) ) [18] => Array ( [name] => minicolors [version] => 1.0 [file] => vendor/minicolors/minicolors.js [dependency] => Array ( [0] => jquery ) ) [19] => Array ( [name] => administration [version] => 1.0 [file] => javascript/index.js [dependency] => Array ( [0] => jquery [1] => bootstrap [2] => checkbox [3] => datepicker [4] => nanobar [5] => owlcarousel [6] => selectmultiple [7] => selectric [8] => sortable [9] => uisortableanimation [10] => summernote [11] => validation [12] => jrate [13] => retina [14] => confirmation [15] => bootstrapfilestyle [16] => minicolors ) ) ) 

Спасибо

Вероятно, существует несколько различных подходов к решению этого. Здесь я перебираю массив скриптов, удаляя любые зависимости, которые уже находятся в выходном массиве, перед добавлением каких-либо скриптов без каких-либо дополнительных зависимостей.

Я не тестировал его на уничтожение, но он работает с вашим примером.

 $sorted = []; while ($count = count($scripts)) { // Remove any met dependencies. foreach ($scripts as $script_id => $script) { if (isset($script["dependency"])) { foreach ($script["dependency"] as $dep_id => $dep) { if (isset($sorted[$dep])) { unset($scripts[$script_id]["dependency"][$dep_id]); } } if (!count($scripts[$script_id]["dependency"])) { unset($scripts[$script_id]["dependency"]); } } } // Add scripts with no more dependencies to the output array. foreach ($scripts as $script_id => $script) { if (!isset($script["dependency"])) { $sorted[$script["name"]] = $script; unset($scripts[$script_id]); } } if (count($scripts) == $count) { die("Unresolvable dependency"); } } var_dump(array_values($sorted)); /* array (size=5) 0 => array (size=3) 'name' => string 'jquery' (length=6) 'version' => string '1.1' (length=3) 'file' => string 'vendor/jquery/jquery.js' (length=23) 1 => array (size=3) 'name' => string 'cookie' (length=6) 'version' => string '1.0' (length=3) 'file' => string 'vendor/cookie/cookie.js' (length=23) 2 => array (size=3) 'name' => string 'bootstrap' (length=9) 'version' => string '1.0' (length=3) 'file' => string 'vendor/bootstrap/js/bootstrap.js' (length=32) 3 => array (size=3) 'name' => string 'checkbox' (length=8) 'version' => string '1.0' (length=3) 'file' => string 'vendor/checkbox/checkbox.js' (length=27) 4 => array (size=3) 'name' => string 'admin' (length=5) 'version' => string '1.0' (length=3) 'file' => string 'vendor/admin/code.js' (length=20) */ эта $sorted = []; while ($count = count($scripts)) { // Remove any met dependencies. foreach ($scripts as $script_id => $script) { if (isset($script["dependency"])) { foreach ($script["dependency"] as $dep_id => $dep) { if (isset($sorted[$dep])) { unset($scripts[$script_id]["dependency"][$dep_id]); } } if (!count($scripts[$script_id]["dependency"])) { unset($scripts[$script_id]["dependency"]); } } } // Add scripts with no more dependencies to the output array. foreach ($scripts as $script_id => $script) { if (!isset($script["dependency"])) { $sorted[$script["name"]] = $script; unset($scripts[$script_id]); } } if (count($scripts) == $count) { die("Unresolvable dependency"); } } var_dump(array_values($sorted)); /* array (size=5) 0 => array (size=3) 'name' => string 'jquery' (length=6) 'version' => string '1.1' (length=3) 'file' => string 'vendor/jquery/jquery.js' (length=23) 1 => array (size=3) 'name' => string 'cookie' (length=6) 'version' => string '1.0' (length=3) 'file' => string 'vendor/cookie/cookie.js' (length=23) 2 => array (size=3) 'name' => string 'bootstrap' (length=9) 'version' => string '1.0' (length=3) 'file' => string 'vendor/bootstrap/js/bootstrap.js' (length=32) 3 => array (size=3) 'name' => string 'checkbox' (length=8) 'version' => string '1.0' (length=3) 'file' => string 'vendor/checkbox/checkbox.js' (length=27) 4 => array (size=3) 'name' => string 'admin' (length=5) 'version' => string '1.0' (length=3) 'file' => string 'vendor/admin/code.js' (length=20) */ не $sorted = []; while ($count = count($scripts)) { // Remove any met dependencies. foreach ($scripts as $script_id => $script) { if (isset($script["dependency"])) { foreach ($script["dependency"] as $dep_id => $dep) { if (isset($sorted[$dep])) { unset($scripts[$script_id]["dependency"][$dep_id]); } } if (!count($scripts[$script_id]["dependency"])) { unset($scripts[$script_id]["dependency"]); } } } // Add scripts with no more dependencies to the output array. foreach ($scripts as $script_id => $script) { if (!isset($script["dependency"])) { $sorted[$script["name"]] = $script; unset($scripts[$script_id]); } } if (count($scripts) == $count) { die("Unresolvable dependency"); } } var_dump(array_values($sorted)); /* array (size=5) 0 => array (size=3) 'name' => string 'jquery' (length=6) 'version' => string '1.1' (length=3) 'file' => string 'vendor/jquery/jquery.js' (length=23) 1 => array (size=3) 'name' => string 'cookie' (length=6) 'version' => string '1.0' (length=3) 'file' => string 'vendor/cookie/cookie.js' (length=23) 2 => array (size=3) 'name' => string 'bootstrap' (length=9) 'version' => string '1.0' (length=3) 'file' => string 'vendor/bootstrap/js/bootstrap.js' (length=32) 3 => array (size=3) 'name' => string 'checkbox' (length=8) 'version' => string '1.0' (length=3) 'file' => string 'vendor/checkbox/checkbox.js' (length=27) 4 => array (size=3) 'name' => string 'admin' (length=5) 'version' => string '1.0' (length=3) 'file' => string 'vendor/admin/code.js' (length=20) */ эта $sorted = []; while ($count = count($scripts)) { // Remove any met dependencies. foreach ($scripts as $script_id => $script) { if (isset($script["dependency"])) { foreach ($script["dependency"] as $dep_id => $dep) { if (isset($sorted[$dep])) { unset($scripts[$script_id]["dependency"][$dep_id]); } } if (!count($scripts[$script_id]["dependency"])) { unset($scripts[$script_id]["dependency"]); } } } // Add scripts with no more dependencies to the output array. foreach ($scripts as $script_id => $script) { if (!isset($script["dependency"])) { $sorted[$script["name"]] = $script; unset($scripts[$script_id]); } } if (count($scripts) == $count) { die("Unresolvable dependency"); } } var_dump(array_values($sorted)); /* array (size=5) 0 => array (size=3) 'name' => string 'jquery' (length=6) 'version' => string '1.1' (length=3) 'file' => string 'vendor/jquery/jquery.js' (length=23) 1 => array (size=3) 'name' => string 'cookie' (length=6) 'version' => string '1.0' (length=3) 'file' => string 'vendor/cookie/cookie.js' (length=23) 2 => array (size=3) 'name' => string 'bootstrap' (length=9) 'version' => string '1.0' (length=3) 'file' => string 'vendor/bootstrap/js/bootstrap.js' (length=32) 3 => array (size=3) 'name' => string 'checkbox' (length=8) 'version' => string '1.0' (length=3) 'file' => string 'vendor/checkbox/checkbox.js' (length=27) 4 => array (size=3) 'name' => string 'admin' (length=5) 'version' => string '1.0' (length=3) 'file' => string 'vendor/admin/code.js' (length=20) */ не $sorted = []; while ($count = count($scripts)) { // Remove any met dependencies. foreach ($scripts as $script_id => $script) { if (isset($script["dependency"])) { foreach ($script["dependency"] as $dep_id => $dep) { if (isset($sorted[$dep])) { unset($scripts[$script_id]["dependency"][$dep_id]); } } if (!count($scripts[$script_id]["dependency"])) { unset($scripts[$script_id]["dependency"]); } } } // Add scripts with no more dependencies to the output array. foreach ($scripts as $script_id => $script) { if (!isset($script["dependency"])) { $sorted[$script["name"]] = $script; unset($scripts[$script_id]); } } if (count($scripts) == $count) { die("Unresolvable dependency"); } } var_dump(array_values($sorted)); /* array (size=5) 0 => array (size=3) 'name' => string 'jquery' (length=6) 'version' => string '1.1' (length=3) 'file' => string 'vendor/jquery/jquery.js' (length=23) 1 => array (size=3) 'name' => string 'cookie' (length=6) 'version' => string '1.0' (length=3) 'file' => string 'vendor/cookie/cookie.js' (length=23) 2 => array (size=3) 'name' => string 'bootstrap' (length=9) 'version' => string '1.0' (length=3) 'file' => string 'vendor/bootstrap/js/bootstrap.js' (length=32) 3 => array (size=3) 'name' => string 'checkbox' (length=8) 'version' => string '1.0' (length=3) 'file' => string 'vendor/checkbox/checkbox.js' (length=27) 4 => array (size=3) 'name' => string 'admin' (length=5) 'version' => string '1.0' (length=3) 'file' => string 'vendor/admin/code.js' (length=20) */ эта $sorted = []; while ($count = count($scripts)) { // Remove any met dependencies. foreach ($scripts as $script_id => $script) { if (isset($script["dependency"])) { foreach ($script["dependency"] as $dep_id => $dep) { if (isset($sorted[$dep])) { unset($scripts[$script_id]["dependency"][$dep_id]); } } if (!count($scripts[$script_id]["dependency"])) { unset($scripts[$script_id]["dependency"]); } } } // Add scripts with no more dependencies to the output array. foreach ($scripts as $script_id => $script) { if (!isset($script["dependency"])) { $sorted[$script["name"]] = $script; unset($scripts[$script_id]); } } if (count($scripts) == $count) { die("Unresolvable dependency"); } } var_dump(array_values($sorted)); /* array (size=5) 0 => array (size=3) 'name' => string 'jquery' (length=6) 'version' => string '1.1' (length=3) 'file' => string 'vendor/jquery/jquery.js' (length=23) 1 => array (size=3) 'name' => string 'cookie' (length=6) 'version' => string '1.0' (length=3) 'file' => string 'vendor/cookie/cookie.js' (length=23) 2 => array (size=3) 'name' => string 'bootstrap' (length=9) 'version' => string '1.0' (length=3) 'file' => string 'vendor/bootstrap/js/bootstrap.js' (length=32) 3 => array (size=3) 'name' => string 'checkbox' (length=8) 'version' => string '1.0' (length=3) 'file' => string 'vendor/checkbox/checkbox.js' (length=27) 4 => array (size=3) 'name' => string 'admin' (length=5) 'version' => string '1.0' (length=3) 'file' => string 'vendor/admin/code.js' (length=20) */ не $sorted = []; while ($count = count($scripts)) { // Remove any met dependencies. foreach ($scripts as $script_id => $script) { if (isset($script["dependency"])) { foreach ($script["dependency"] as $dep_id => $dep) { if (isset($sorted[$dep])) { unset($scripts[$script_id]["dependency"][$dep_id]); } } if (!count($scripts[$script_id]["dependency"])) { unset($scripts[$script_id]["dependency"]); } } } // Add scripts with no more dependencies to the output array. foreach ($scripts as $script_id => $script) { if (!isset($script["dependency"])) { $sorted[$script["name"]] = $script; unset($scripts[$script_id]); } } if (count($scripts) == $count) { die("Unresolvable dependency"); } } var_dump(array_values($sorted)); /* array (size=5) 0 => array (size=3) 'name' => string 'jquery' (length=6) 'version' => string '1.1' (length=3) 'file' => string 'vendor/jquery/jquery.js' (length=23) 1 => array (size=3) 'name' => string 'cookie' (length=6) 'version' => string '1.0' (length=3) 'file' => string 'vendor/cookie/cookie.js' (length=23) 2 => array (size=3) 'name' => string 'bootstrap' (length=9) 'version' => string '1.0' (length=3) 'file' => string 'vendor/bootstrap/js/bootstrap.js' (length=32) 3 => array (size=3) 'name' => string 'checkbox' (length=8) 'version' => string '1.0' (length=3) 'file' => string 'vendor/checkbox/checkbox.js' (length=27) 4 => array (size=3) 'name' => string 'admin' (length=5) 'version' => string '1.0' (length=3) 'file' => string 'vendor/admin/code.js' (length=20) */