【问题标题】:Is there a way to combine, following certain criteria, a max of 3 multidimensional array's with OOP PHP objects into one array?有没有办法按照一定的标准将最多 3 个多维数组与 OOP PHP 对象组合成一个数组?
【发布时间】:2019-12-28 22:32:08
【问题描述】:

已编辑: 更短更通用的版本:

我正在寻找一种在 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。

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

【问题讨论】:

  • 恕我直言,这里作为一个问题的问题是,对于大多数人来说,有很多细节想要涉足它。正因为如此,您可能很难得到答案。
  • 谢谢你,我是一个在这里发帖的新手......也许你可以提供一些建议,我可以如何缩短问题,好吗?我害怕没有所有信息,我的要求可能不够清楚......
  • @NigelRen 我试图让问题更简短更通用,这样更好吗?

标签: php sorting oop object multidimensional-array


【解决方案1】:

这就是我要解决的方法,这只是一个循环,最后是一个排序,以根据 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;
}

【讨论】:

  • 非常感谢@cOle2!我需要摆弄它,但让它起作用了!我需要在 foreach 中进行额外的缩进,因为它试图找到高级别的 urlLink。因为我正在使用类,所以我需要将类的 usort 请求扩展为,这对我来说是新事物,所以我花了更长的时间来找到答案... usort($this->results, array($这个,“sortByRank”));谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多