Split array into unique pairs

前端 未结 6 1377
感动是毒
感动是毒 2021-01-06 02:05

Say i start with a simple array (which could be theoretically of any length):

$ids  = array(1,2,3,4);

What it the best solution for splitti

6条回答
  •  情话喂你
    2021-01-06 02:54

    Probably not the best solution

    $ids  = array(1,2,3,4);
    
    $pairs = array();
    foreach($ids as $key => $data){
        foreach($ids as $subkey => $subdata){
            if( $subkey != $key){
                if(!in_array(array($subdata, $data) , $pairs) ){
                    $pairs[] = array($data, $subdata);
                }
            }
        }
    }
    

    Anyway it works

提交回复
热议问题