gpt4 book ai didi

php - 根据另一个数组键值对数组进行排序

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:54 24 4
gpt4 key购买 nike

谁能给我一个例子,说明如何根据每个数组的依赖键对这个数组进行排序。我希望数组按照依赖项的顺序排列,所以首先是 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)
*/

关于php - 根据另一个数组键值对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37927863/

24 4 0
文章推荐: php - #php - "page expire"而不是 "session expire"
文章推荐: html - 标签的任何替代方案,将 html 呈现为纯文本</a> </div> <div> 文章推荐: <a class="a-tag" href="/article/19/1239787/detail.html" target="_blank">javascript - 如何使用 javascript 和 jQuery 制作列表子项?</a> </div> <div> 文章推荐: <a class="a-tag" href="/article/19/1239786/detail.html" target="_blank">php - 根据php中的一个邮政编码找到最近的邮政编码</a> </div> </div> <div class="content-p"> <ul class="like-article"> <li> <a class="a-tag" href="/article/23/5211869/detail.html" target="_blank">php - 输出 PHP 分隔符 (<?php, ?>),PHP 不解释分隔符</a> <p>我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使</p> </li> <li> <a class="a-tag" href="/article/23/8252855/detail.html" target="_blank">php - PHP 是否具有确定与给定 PHP 脚本兼容的最旧 PHP 版本的内置方法?还是会永远?</a> <p>我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.</p> </li> <li> <a class="a-tag" href="/article/23/6498818/detail.html" target="_blank">php - 在没有 php 框架的情况下将 php 框架用于现有的 php 应用程序是个好主意吗?</a> <p>我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几</p> </li> <li> <a class="a-tag" href="/article/23/5549844/detail.html" target="_blank">php - 如何在同一 php 页面上的其他 php 脚本中使用 php 变量</a> <p>下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name) </p> </li> <li> <a class="a-tag" href="/article/23/8354225/detail.html" target="_blank">php - PHP 重定向后 PHP 是否继续执行?</a> <p>我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见</p> </li> <li> <a class="a-tag" href="/article/23/8140380/detail.html" target="_blank">php - 如何避免在 php 文件中写入 <?php</a> <p>我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入 <?php,我们在Stack Overflow上找</p> </li> <li> <a class="a-tag" href="/article/23/7749807/detail.html" target="_blank">php - 需要建议通过 php 编辑 php</a> <p>我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观</p> </li> <li> <a class="a-tag" href="/article/23/7273178/detail.html" target="_blank">php - 无法发布 “PHP file” PHP</a> <p>我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S</p> </li> <li> <a class="a-tag" href="/article/23/6931040/detail.html" target="_blank">php - PHP 是否有生成有效 PHP 表达式的打印函数?</a> <p>我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1] </p> </li> <li> <a class="a-tag" href="/article/23/6696910/detail.html" target="_blank">php - PHP 数组键有大小限制吗? PHP 数组一般有哪些限制?</a> <p>这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道 </p> </li> <li> <a class="a-tag" href="/article/23/6424564/detail.html" target="_blank">php - PHP 是否有生成有效 PHP 表达式的打印函数?</a> <p>我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1] </p> </li> <li> <a class="a-tag" href="/article/23/4763704/detail.html" target="_blank">php - 让函数在 php 刷新时执行 (PHP)</a> <p>关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques</p> </li> <li> <a class="a-tag" href="/article/23/4444478/detail.html" target="_blank">php - 按时间段显示菜单 PHP PHP MySQL</a> <p>我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_</p> </li> <li> <a class="a-tag" href="/article/23/4442168/detail.html" target="_blank">php - 将参数发送到嵌套在 PHP 页面内的动态 PHP</a> <p>我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php</p> </li> <li> <a class="a-tag" href="/article/23/4439353/detail.html" target="_blank">php - 文本区域 -> php -> mysql -> php</a> <p>我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET</p> </li> <li> <a class="a-tag" href="/article/23/4394844/detail.html" target="_blank">php - 我如何将一个 php 的值解析为另一个 php</a> <p>首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id</p> </li> <li> <a class="a-tag" href="/article/23/4325685/detail.html" target="_blank">php - php 中的安全首选项、php 函数来清理输入</a> <p>我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va</p> </li> <li> <a class="a-tag" href="/article/23/4169244/detail.html" target="_blank">php - HTML/PHP 登录重定向到 php</a> <p>我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、</p> </li> <li> <a class="a-tag" href="/article/23/3619344/detail.html" target="_blank">php - onClick php PHP 函数然后重定向</a> <p>我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){ </p> </li> <li> <a class="a-tag" href="/article/22/3169665/detail.html" target="_blank">php - 有没有办法从 PHP 代码加载的文件中解析 PHP?</a> <p>我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l</p> </li> </ul> </div> </div> <div class="resource col-xs-3 col-sm-3 col-md-3 col-lg-3"> <div class="content-p content-p-comment"> <div class="phone-current phone-current-float"> <img alt="" src="/images/phone/manphone.jpeg"> </div> <div class="phone-current-float phone-current-style"> 搜寻专家 </div> <div class="phone-current-summary"> <span><strong>个人简介</strong></span> <p> 我是一名优秀的程序员,十分优秀! </p> </div> </div> <div class="content-p content-p-comment"> <article class="p-list"> <div class="art-margin" style="border-bottom: 1px solid #f3f0f0; padding-bottom: 5px;"> <strong>作者热门文章</strong> </div> <ul class="recomment-list-user"> <li><a class="a-tag" href="/article/19/1071999/detail.html" target="_blank">Java 双重比较</a></li> <li><a class="a-tag" href="/article/19/1071998/detail.html" target="_blank">java - 比较器与 Apache BeanComparator</a></li> <li><a class="a-tag" href="/article/19/1071997/detail.html" target="_blank">Objective-C 完成 block 导致额外的方法调用?</a></li> <li><a class="a-tag" href="/article/19/1071996/detail.html" target="_blank">database - RESTful URI 是否应该公开数据库主键?</a></li> </ul> </article> </div> <div class="content-p content-p-comment"> <article class="p-list"> <div class="art-margin" style="border-bottom: 1px solid #f3f0f0; padding-bottom: 5px;"> <strong>滴滴打车优惠券免费领取</strong> </div> <img alt="滴滴打车优惠券" src="/images/ad/didiad.png" width="210px" onclick="window.open('/ad/didi', '_blank')"> </article> </div> <div class="content-p content-p-comment"> <article class="p-list"> <div class="art-margin" style="border-bottom: 1px solid #f3f0f0; padding-bottom: 5px;"> <strong>全站热门文章</strong> </div> <ul class="recomment-list-user"> <li><a class="a-tag" href="/article/92/8826031/detail.html" target="_blank">Serilog文档翻译系列(五)-编写日志事件</a></li> <li><a class="a-tag" href="/article/92/8826030/detail.html" target="_blank">k8sStorageClass存储类</a></li> <li><a class="a-tag" href="/article/92/8826029/detail.html" target="_blank">线程状态转换?创建线程的几种方式?线程如何停止?</a></li> <li><a class="a-tag" href="/article/92/8826028/detail.html" target="_blank">小白必看!入门嵌入式你需要了解这些!</a></li> <li><a class="a-tag" href="/article/92/8826027/detail.html" target="_blank">项目实战:Qt+OSG爆破动力学仿真三维引擎测试工具v1.1.0(加载.K模型,子弹轨迹模拟动画,支持windows、linux、国产麒麟系统)</a></li> <li><a class="a-tag" href="/article/92/8826026/detail.html" target="_blank">linux操作系统和文件系统,命令(上)</a></li> <li><a class="a-tag" href="/article/92/8826025/detail.html" target="_blank">ModbusRTU通信协议报文剖析</a></li> <li><a class="a-tag" href="/article/92/8826024/detail.html" target="_blank">PasteForm最佳CRUD实践,实际案例PasteTemplate详解(一)</a></li> <li><a class="a-tag" href="/article/92/8826023/detail.html" target="_blank">SqlEs-像使用数据库一样使用Elasticsearch</a></li> <li><a class="a-tag" href="/article/92/8826022/detail.html" target="_blank">macM1,M2,M3芯片踩坑nodejsrubybrew</a></li> </ul> </article> </div> </div> </div> </div> <div class="foot-font" style="border-top: 1px solid #f3f0f0; margin: auto; padding: 15px; background-color: #474443" align="center"> <a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank"><span class="color-txt-foot">Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号</span></a> <br/> <a href="/" target="_blank"><span class="color-txt-foot">广告合作:1813099741@qq.com</span></a> <a href="http://www.6ren.com" target="_blank"><span class="color-txt-foot">6ren.com</span></a> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d1cb9c185f1642d6f07e22cafa330c45"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d46c26b2162aface49b8acf6cb7025e1"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>