简单说明
我定义咯一个数组,
数组的key表示当前步的ID,下一步的ID对应的value...
我要生成一个这个新的数组
例子:
array(1 => int 7
7 => int 11
8 => int 7
9 => int 8
10 => int 11
11 => int 12
12 => NULL
)
结果就是
array(
1=>7
7=>11
11=>12
12=>NULL
)
当前知道开头位为1.
求算法...
回复讨论(解决方案)
$a = array( 1 => 7, 7 => 11, 8 => 7, 9 => 8, 10 => 11, 11 => 12, 12 => NULL,);$i = 1;while(isset($a[$i])) { $r[$i] = $a[$i]; $i = $a[$i];}$r[$i] = $a[$i]; var_export($r);array (
1 => 7,
7 => 11,
11 => 12,
12 => NULL,
)
$map = array( 1 => 7, 7 => 11, 8 => 7, 9 => 8, 10 => 11, 11 => 12, 12 => NULL);function getroad($map){ $key = 1; $result = array(); while($key!=NULL){ $result[$key] = $map[$key]; $key = $map[$key]; } return $result;}$result = getroad($map);var_export($result);
array (
1 => 7,
7 => 11,
11 => 12,
12 => NULL,
)
$a = array( 1 => 7, 7 => 11, 8 => 7, 9 => 8, 10 => 11, 11 => 12, 12 => NULL,);$i = 1;while(isset($a[$i])) { $r[$i] = $a[$i]; $i = $a[$i];}$r[$i] = $a[$i]; var_export($r);array (
1 => 7,
7 => 11,
11 => 12,
12 => NULL,
)
有点点问题
如果key=value就会死循环...
不过这个可以让通过程序让key!=value
$map = array( 1 => 7, 7 => 11, 8 => 7, 9 => 8, 10 => 11, 11 => 12, 12 => NULL);function getroad($map){ $key = 1; $result = array(); while($key!=NULL){ $result[$key] = $map[$key]; $key = $map[$key]; } return $result;}$result = getroad($map);var_export($result);
array (
1 => 7,
7 => 11,
11 => 12,
12 => NULL,
)
这个也会有死循环的问题
$map = array(
1 => 11,
7 => 11,
8 => 7,
9 => 8,
10 => 11,
11 => 8,
12 => NULL
);
你之前的结束条件是NULL,而你却写一个死循环的map,明显和约定的条件不同。
如果死循环,那么结束条件是什么?是否出现循环就直接结束?
对的...
直接出现循环就结束...
我自己慢慢折腾出来咯..不是很好看
$a = array( 7 => 11, 1 => 11, 8 => 7, 9 => 8, 10 => 11, 11 => 8, 12 => NULL,);$firstID = 1;$nextID = 11; //这里程序可以找到是11//将第一位提前$newReturnTemp[$firstID] = $nextID;if (empty($nextID)) { return array($a[$firstID]);} else { //重新组建数组,并让开始为为第一位 foreach ($a as $key => $value) { $newReturnTemp[$key] = $value; } unset($key); unset($value); //获取数组的数目 $stepCount = count($a); //提取所有键值 $stepArray = array_keys($newReturnTemp); //让判断位为第一位 $temp = $firstID; //组件要返回的数组第一位 $newnew[$firstID] = $nextID; //将键值进行循环 for ($i = 0; $i < $stepCount; $i++) { //默认从第一步开始, if ($temp == $stepArray[$i]) { //键值数组循环后面的值 for ($j = $i + 1; $j < $stepCount; $j++) { //判断是否找到下一级的值,依据$a的内容 if ($newReturnTemp[$stepArray[$i]] == $stepArray[$j]) { //找到值,写入需要返回的数组 $newnew[$temp] = $stepArray[$j]; //设定下一个判断值 $temp = $stepArray[$j]; //将键值循环的下一位与当前所返回的下一级值对换,并保证下一次判断是从下一级开始 $tt = $stepArray[$j]; $stepArray[$j] = $stepArray[$i + 1]; $stepArray[$i + 1] = $tt; unset($tt); } } } } //将返回排序好的数组,并依据排队内容一次取出相对应的数据,这里是用$a来做演示的... foreach ($newnew as $key => $value) { $reReturn[$key] = $a[$key]; $last = $value; } //将默认最后一位没有找到的值或者是不正确的下一级补进数组...(可以不需要这一步) if (isset($a[$last])) { $reReturn[$last] = $a[$last]; }}var_dump($reReturn);
支持自己动手的,呵呵。
先实现,再优化。

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显著提升PHP应用的性能。

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能