有没有办法按照某些标准将最多 3 个多维数组与 OOP PHP 对象组合成一个数组?

 更短更通用的版本:


我在 PHP 中寻找一种算法来将最多 3 个多维对象数组组合成一个多维对象数组。


但是我需要一些特定的东西,所以简单的 array_merge 是不够的。


如果有重复,则必须调整排名,因此该结果项需要在数组中上升。

对于其他结果:它需要将相同高度的结果相互编织,而不是像 array_merge 那样将结果相互粘贴。

此外,我还需要考虑到,稍后应用程序可能会通过对结果的评级进行扩展,因此排名也可能会受到基于评级的适应性调整的主观影响。

Array1(

Object A

”Title” => ”A”

”Rank” => 0


Object B

”Title” => ”B”

”Rank” => 1


Object C

”Title” => ”C”

”Rank” => 2


Object S

”Title” =>” S”

”Rank” => 2

)

——


Array 2 (

Object X

”Title” => X

”Rank” => 0


Object Z

”Title” => ”Z”

”Rank” => 1


Object C

”Title” =>” C”

”Rank” => 2

——


Array3(

Object Z

”Title” => ”Z”

”Rank” => 0


Object A

”Title” => ”A”

”Rank” => 1


Object C

”Title” => ”C”

”Rank” => 2

)

- 结果应该是:


combinedArray(

Object C // is found by all tree

”Title” => ”C”

”Rank” => 2


Object A // is found in 2

”Title” => ”A”

”Rank” => 0


Object Z // is found in 2

”Title” => ”Z”

”Rank” => 0


Object X

”Title” => X

”Rank” => 0


Object B

”Title” => ”B”

”Rank” => 1


Object S

”Title” =>” S”

”Rank” => 2

)

__


首选的回答语言是 PHP。


预先感谢您的帮助并花时间阅读所有这些内容!


MMMHUHU
浏览 135回答 1
1回答

慕桂英3389331

这就是我将如何解决它,这只是一个循环和最后的 usort 以根据 newRank 设置顺序。另外我假设重复的只是网址。输出链接在这里:http : //codepad.org/ClubESQ6<?php//google$res1 = array();$res1[0] = array('id'=>null, 'title'=>'Owl - Wikipedia', 'urlLink'=>'https://en.wikipedia.org/wiki/Owl', 'currentRank'=>0, 'newRank'=>null);$res1[1] = array('id'=>null, 'title'=>'Owl (Marvel Comics) - Wikipedia', 'urlLink'=>'https://en.wikipedia.org/wiki/Owl_(Marvel_Comics)', 'currentRank'=>1, 'newRank'=>null);$res1[2] = array('id'=>null, 'title'=>'Owl Pharaoh - Wikipedia', 'urlLink'=>'https://en.wikipedia.org/wiki/Owl_Pharaoh', 'currentRank'=>2, 'newRank'=>null);$res1[3] = array('id'=>null, 'title'=>'Owl City - Wikipedia', 'urlLink'=>'https://en.wikipedia.org/wiki/Owl_City', 'currentRank'=>3, 'newRank'=>null);$res1[4] = array('id'=>null, 'title'=>'Owl (Nguyen Hung Cuong) | Happy Folding', 'urlLink'=>'https://www.happyfolding.com/gallery-nguyen-owl', 'currentRank'=>1, 'newRank'=>null);//bing$res2 = array();$res2[0] = array('id'=>null, 'title'=>'OWL/TV - Wikipedia', 'urlLink'=>'https://en.wikipedia.org/wiki/OWL/TV', 'currentRank'=>0, 'newRank'=>null);$res2[1] = array('id'=>null, 'title'=>'Owl (Nguyen Hung Cuong) | Happy Folding', 'urlLink'=>'https://www.happyfolding.com/gallery-nguyen-owl', 'currentRank'=>1, 'newRank'=>null);$res2[2] = array('id'=>null, 'title'=>'Owl Pharaoh - Wikipedia', 'urlLink'=>'https://en.wikipedia.org/wiki/Owl_Pharaoh', 'currentRank'=>2, 'newRank'=>null);$res2[3] = array('id'=>null, 'title'=>'Owl "Nightwatch" (Kay Kraschewski a.k.a. "Akugami ...', 'urlLink'=>'https://www.happyfolding.com/instructions-kraschewski-nightwatch', 'currentRank'=>3, 'newRank'=>null);$res2[4] = array('id'=>null, 'title'=>'Owl - Wikipedia', 'urlLink'=>'https://en.wikipedia.org/wiki/Owl', 'currentRank'=>0, 'newRank'=>null);//duckduckgo$res3 = array();$res3[0] = array('id'=>null, 'title'=>'OWL // Purdue Writing Lab', 'urlLink'=>'https://owl.purdue.edu', 'currentRank'=>0, 'newRank'=>null);$res3[1] = array('id'=>null, 'title'=>'Owl - Wikipedia', 'urlLink'=>'https://en.wikipedia.org/wiki/Owl', 'currentRank'=>1, 'newRank'=>null);$res3[2] = array('id'=>null, 'title'=>'Owl | bird | Britannica.com', 'urlLink'=>'https://www.britannica.com/animal/owl', 'currentRank'=>2, 'newRank'=>null);$res3[3] = array('id'=>null, 'title'=>'The Owl Pages - Information about Owls', 'urlLink'=>'https://www.owlpages.com/owls/', 'currentRank'=>3, 'newRank'=>null);$results = mergeResults($res1, $res2, $res3);echo '<pre>', print_r($results, true), '</pre>';function mergeResults() {&nbsp; &nbsp; $results = array(); //array for our final resulsts&nbsp; &nbsp; $numargs = func_num_args();&nbsp; &nbsp; $arg_list = func_get_args();&nbsp; &nbsp; for ($i = 0; $i < $numargs; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $resultSet = $arg_list[$i];&nbsp; &nbsp; &nbsp; &nbsp; foreach($resultSet as $rs) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //is dupe, decrement rank&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($results[$rs['urlLink']])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentRank = $results[$rs['urlLink']]['newRank'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newRank = $currentRank - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results[$rs['urlLink']]['newRank'] = $newRank;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rs['newRank']&nbsp; = $rs['currentRank'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results[$rs['urlLink']] = $rs;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; usort($results, "sortByRank");&nbsp; &nbsp; return $results;}function sortByRank($a, $b) {&nbsp; &nbsp; if($a["newRank"] == $b["newRank"]) {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; return ($a["newRank"] < $b["newRank"]) ? -1 : 1;}
打开App,查看更多内容
随时随地看视频慕课网APP