1. <legend id='xDtlp'><style id='xDtlp'><dir id='xDtlp'><q id='xDtlp'></q></dir></style></legend>

        <bdo id='xDtlp'></bdo><ul id='xDtlp'></ul>
    1. <tfoot id='xDtlp'></tfoot>
      <i id='xDtlp'><tr id='xDtlp'><dt id='xDtlp'><q id='xDtlp'><span id='xDtlp'><b id='xDtlp'><form id='xDtlp'><ins id='xDtlp'></ins><ul id='xDtlp'></ul><sub id='xDtlp'></sub></form><legend id='xDtlp'></legend><bdo id='xDtlp'><pre id='xDtlp'><center id='xDtlp'></center></pre></bdo></b><th id='xDtlp'></th></span></q></dt></tr></i><div id='xDtlp'><tfoot id='xDtlp'></tfoot><dl id='xDtlp'><fieldset id='xDtlp'></fieldset></dl></div>

      <small id='xDtlp'></small><noframes id='xDtlp'>

      排序依赖于另一个数组键值的数组

      时间:2024-08-10
    2. <i id='qvWIq'><tr id='qvWIq'><dt id='qvWIq'><q id='qvWIq'><span id='qvWIq'><b id='qvWIq'><form id='qvWIq'><ins id='qvWIq'></ins><ul id='qvWIq'></ul><sub id='qvWIq'></sub></form><legend id='qvWIq'></legend><bdo id='qvWIq'><pre id='qvWIq'><center id='qvWIq'></center></pre></bdo></b><th id='qvWIq'></th></span></q></dt></tr></i><div id='qvWIq'><tfoot id='qvWIq'></tfoot><dl id='qvWIq'><fieldset id='qvWIq'></fieldset></dl></div>

    3. <tfoot id='qvWIq'></tfoot>

        <legend id='qvWIq'><style id='qvWIq'><dir id='qvWIq'><q id='qvWIq'></q></dir></style></legend>
        • <small id='qvWIq'></small><noframes id='qvWIq'>

            <tbody id='qvWIq'></tbody>
                <bdo id='qvWIq'></bdo><ul id='qvWIq'></ul>

                本文介绍了排序依赖于另一个数组键值的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                谁能告诉我如何根据每个数组的依赖键对该数组进行排序的示例.我希望数组按依赖顺序排列,所以首先是 jquery,然后是 cookie、bootstrap、checkbox、admin.我看过其他帖子,但它们对我来说没有意义.这是整个数组的一小部分,数组可以是任意顺序和长度.

                Can anyone show me an example of how to sort this array dependent on the dependency key of each array. I would like the array to be in order of the dependency so jquery first then cookie,bootstrap,checkbox,admin. I have looked at other posts but they did not make sense to me. This is a small piece of the full array, the array might be in any order and length.

                谁能给我看一段代码可以做到这一点.

                Can anyone show me a snippet of code that would do this.

                         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
                                            )
                
                                    )
                
                            )
                

                谢谢

                推荐答案

                可能有许多不同的方法来解决这个问题.在这里,我循环遍历脚本数组,在添加任何没有进一步依赖关系的脚本之前删除输出数组中已经存在的所有依赖项.

                There are probably a number of different approaches to solving this. Here I loop over the array of scripts, removing any dependencies that are already in the output array before adding any scripts with no further dependencies.

                我没有对它进行破坏测试,但它适用于您的示例.

                I didn't test it to destruction but it works with your example.

                $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)
                 */
                

                这篇关于排序依赖于另一个数组键值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:作曲家 - 指定 `composer.json` 路径 下一篇:如何在 PHP 中获取客户端 IP 地址

                相关文章

                <tfoot id='Kx5Oc'></tfoot>

                  <bdo id='Kx5Oc'></bdo><ul id='Kx5Oc'></ul>
                <i id='Kx5Oc'><tr id='Kx5Oc'><dt id='Kx5Oc'><q id='Kx5Oc'><span id='Kx5Oc'><b id='Kx5Oc'><form id='Kx5Oc'><ins id='Kx5Oc'></ins><ul id='Kx5Oc'></ul><sub id='Kx5Oc'></sub></form><legend id='Kx5Oc'></legend><bdo id='Kx5Oc'><pre id='Kx5Oc'><center id='Kx5Oc'></center></pre></bdo></b><th id='Kx5Oc'></th></span></q></dt></tr></i><div id='Kx5Oc'><tfoot id='Kx5Oc'></tfoot><dl id='Kx5Oc'><fieldset id='Kx5Oc'></fieldset></dl></div>

              • <small id='Kx5Oc'></small><noframes id='Kx5Oc'>

              • <legend id='Kx5Oc'><style id='Kx5Oc'><dir id='Kx5Oc'><q id='Kx5Oc'></q></dir></style></legend>