为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
MMMHUHU 2021-12-03 16:25:28
 更短更通用的版本:我在 PHP 中寻找一种算法来将最多 3 个多维对象数组组合成一个多维对象数组。但是我需要一些特定的东西,所以简单的 array_merge 是不够的。如果有重复,则必须调整排名,因此该结果项需要在数组中上升。对于其他结果:它需要将相同高度的结果相互编织,而不是像 array_merge 那样将结果相互粘贴。此外,我还需要考虑到,稍后应用程序可能会通过对结果的评级进行扩展,因此排名也可能会受到基于评级的适应性调整的主观影响。Array1(Object A”Title” => ”A””Rank” => 0Object B”Title” => ”B””Rank” => 1Object C”Title” => ”C””Rank” => 2Object S”Title” =>” S””Rank” => 2)——Array 2 (Object X”Title” => X”Rank” => 0Object Z”Title” => ”Z””Rank” => 1Object C”Title” =>” C””Rank” => 2) ——Array3(Object Z”Title” => ”Z””Rank” => 0Object A”Title” => ”A””Rank” => 1Object C”Title” => ”C””Rank” => 2)- 结果应该是:combinedArray(Object C // is found by all tree”Title” => ”C””Rank” => 2Object A // is found in 2”Title” => ”A””Rank” => 0Object Z // is found in 2”Title” => ”Z””Rank” => 0Object X”Title” => X”Rank” => 0Object B”Title” => ”B””Rank” => 1Object S”Title” =>” S””Rank” => 2)__首选的回答语言是 PHP。预先感谢您的帮助并花时间阅读所有这些内容!
查看完整描述

1 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

这就是我将如何解决它,这只是一个循环和最后的 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() {

    $results = array(); //array for our final resulsts

    $numargs = func_num_args();

    $arg_list = func_get_args();

    for ($i = 0; $i < $numargs; $i++) {

        $resultSet = $arg_list[$i];

        foreach($resultSet as $rs) {

            //is dupe, decrement rank

            if (isset($results[$rs['urlLink']])) {

                $currentRank = $results[$rs['urlLink']]['newRank'];

                $newRank = $currentRank - 1;

                $results[$rs['urlLink']]['newRank'] = $newRank;

            } else {

                $rs['newRank']  = $rs['currentRank'];

                $results[$rs['urlLink']] = $rs; 

            }

        }

    }


    usort($results, "sortByRank");


    return $results;

}


function sortByRank($a, $b) {

    if($a["newRank"] == $b["newRank"]) {

        return 0;

    }

    return ($a["newRank"] < $b["newRank"]) ? -1 : 1;

}


查看完整回答
反对 回复 2021-12-03
  • 1 回答
  • 0 关注
  • 134 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信