Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php对相似记录进行分组_Php_Postgresql_Loops_Grouping_Records - Fatal编程技术网

用php对相似记录进行分组

用php对相似记录进行分组,php,postgresql,loops,grouping,records,Php,Postgresql,Loops,Grouping,Records,我需要帮助编写php脚本的逻辑,该脚本将数据排序为特定格式 首先,脚本需要遍历每个s1值并ping一个端点,以获得实际引用其他s1记录的ml值(更像)。这是最简单的部分!数据是这样返回的 Table 1 s1 | ml ---------- 1 | - 2 | 3,4 3 | 2,8,9 4 | - 5 | 2 6 | 1 7 | 10 8 | - 9 | - 10 | - 条件1:正如您所见,端点返回

我需要帮助编写php脚本的逻辑,该脚本将数据排序为特定格式

首先,脚本需要遍历每个s1值并ping一个端点,以获得实际引用其他s1记录的ml值(更像)。这是最简单的部分!数据是这样返回的

Table 1
s1  |   ml
----------
1   |   -
2   |   3,4    
3   |   2,8,9
4   |   -
5   |   2
6   |   1
7   |   10
8   |   -
9   |   -
10  |   -
条件1:正如您所见,端点返回s1值的数据,告诉它其他s1记录与其他记录类似,但ml的方向并不总是双向的。有时,如s1=6时,ml值为1,但s1=1时,没有ml值

条件2:再次解释ml记录,看看上面和下面的s1=5(上面)和s1=2+rec=5(下面),这个脚本需要意识到已经有一个s1记录,它的值应该添加到那里

条件3:注意当s1=2,ml=3时是如何存储的,但当s1=3,ml=2时会忽略,因为我们有相反的记录

我基本上希望将所有数据匹配到一个排序的“profile”中,因此它以下面的格式结束,我将存储在另一个“排序”记录的db表中

Table 2
s1  |   rec
----------
2   |   3
2   |   4
2   |   8
2   |   9
2   |   9
2   |   5
6   |   1
7   |   10
这已经折磨了我好几天了,我需要一些高效的东西,因为最终它将处理数百万条记录,我相信有一个简单的解决方案,但我就是不知道如何开始

我尝试了以下方法,但我被卡住了,不知道如何走得更远

public function getrelated($id='', $t=''){

    if($id != ""){
    $get = Easytest::where('s1','=',$id)->get();

        if(count($get) > 0){
            $ret= array();
            foreach($get as $go){
                    $v = explode(",", $go->s2);

                    foreach ($v as $e) {
                        if($e != $t){
                            $ret[$e] = $this->getrelated($e, $id);
                        }
                    }
                }
                if(count($ret) > 0){
                    return $ret;
                }else{
                    return "";
                }

        }else{
                return $id;
        }
     }else{
        return "";
     }

     }

public function easytest(){
    ob_start();
    $a = array(
                array("s1"=>1,"s2"=>implode(",",array()).""),
                array("s1"=>2,"s2"=>implode(",",array(3,4)).","),
                array("s1"=>3,"s2"=>implode(",",array(2,8,9)).","),
                array("s1"=>4,"s2"=>implode(",",array()).""),
                array("s1"=>5,"s2"=>implode(",",array(2)).","),
                array("s1"=>6,"s2"=>implode(",",array(1)).","),
                array("s1"=>7,"s2"=>implode(",",array(10)).","),
                array("s1"=>8,"s2"=>implode(",",array()).""),
                array("s1"=>9,"s2"=>implode(",",array()).""),
                array("s1"=>10,"s2"=>implode(",",array()).""),
                array("s1"=>11,"s2"=>implode(",",array(12)).","),
                array("s1"=>12,"s2"=>implode(",",array(2)).",")
                );

    //return Easytest::insert($a);

    $records = Easytest::all();

    foreach ($records as $record) {

        $id = $record->s1;
        echo "ROW: ".$id." > ";
        $record->s2 = ltrim($record->s2,",");
        $ml = explode(",",$record->s2);
        if(count($ml) >= 1){
            foreach ($ml as $t) {

                echo "RESULT: ".$t." -".print_r($this->getrelated($t, $id), true);
                echo ",\n";

            }
        }
        echo " <br><br>\n\n";

    }

    return ob_get_clean();

}
公共函数getrelated($id='',$t=''){
如果($id!=“”){
$get=Easytest::where('s1','=',$id)->get();
如果(计数($get)>0){
$ret=array();
foreach($get as$go){
$v=爆炸(“,”,$go->s2);
外汇兑换($v为$e){
如果($e!=$t){
$ret[$e]=$this->getrelated($e,$id);
}
}
}
如果(计数($ret)>0){
返回$ret;
}否则{
返回“”;
}
}否则{
返回$id;
}
}否则{
返回“”;
}
}
公共功能easytest(){
ob_start();
$a=数组(
数组(“s1”=>1,“s2”=>introde(“,”,array())。”),
数组(“s1”=>2,“s2”=>内爆(“,”,数组(3,4))。“,”,
数组(“s1”=>3,“s2”=>内爆(“,”,数组(2,8,9)),”,
数组(“s1”=>4,“s2”=>introde(“,”,array())。”),
数组(“s1”=>5,“s2”=>内爆(“,”,数组(2))。“,”,
数组(“s1”=>6,“s2”=>内爆(“,”,数组(1))。“,”,
数组(“s1”=>7,“s2”=>内爆(“,”,数组(10))。“,”,
数组(“s1”=>8,“s2”=>introde(“,”,array())。”),
数组(“s1”=>9,“s2”=>introde(“,”,array())。”),
数组(“s1”=>10,“s2”=>introde(“,”,array())。”),
数组(“s1”=>11,“s2”=>内爆(“,”,数组(12))。“,”,
阵列(“s1”=>12,“s2”=>内爆(“,”,阵列(2))。“,”)
);
//返回Easytest::insert($a);
$records=Easytest::all();
foreach($记录为$记录){
$id=$record->s1;
回显“行:”.$id.“>”;
$record->s2=ltrim($record->s2,“,”);
$ml=爆炸(“,”,$record->s2);
如果(计数($ml)>=1){
foreach($ml作为$t){
echo“RESULT:”.$t.“-”.print\r($this->getrelated($t,$id),true);
回声“,\n”;
}
}
回显“

\n\n”; } 返回ob_get_clean(); }
好的,所以我最终解决了这个问题。。。本质上这是下面的代码; (欢迎:)

您需要像这样调用函数

related(array('searched'=>array(),'tosearch'=>array(13)));
功能:

public function related($input){


    $searched = $input['searched'];

    $ar = array();
    $bits = array();

    if(count($input['tosearch']) != 0){

        $get = Easytest::orWhere(function($query) use ($input)
                        {
                            foreach ($input['tosearch'] as $k=>$v) {
                                    $query->orWhere('s2', 'LIKE', '%,'.$v.',%')->orWhere('s1', '=', $v);
                                }                      
                        })
            ->orderBy('s1', 'ASC')->get();

            foreach ($input['tosearch'] as $k=>$v) {
                                unset($input['tosearch'][$k]);
                                $input['searched'][$v] = $v;
                }


            foreach ($get as $result) {

                $thesebits = explode(",", trim($result->s2,","));
                foreach ($thesebits as $smallbit) {
                    if($smallbit != ""){
                    $bits[] = $smallbit;
                    }
                }

                $bits[] = $result->s1;
                $bits = array_unique($bits);

                foreach ($bits as $k=>$v) {
                        if(($key = array_search($v, $input['searched'])) == false) {
                                $input['tosearch'][$v] = $v; 
                        }else{
                            unset($input['tosearch'][$v]);
                        }

                }

                $input['tosearch'] = array_unique($input['tosearch']);

            }
            return $this->related($input);
    }else{
        return $input;
    }

}

所以我已经走了这么远,现在我需要它来做分类,我真的被卡住了。Eeek