Split array into unique pairs

前端 未结 6 1370
感动是毒
感动是毒 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:43

    Sweet solution, Nev Stokes! I changed the 'while' statement to avoid the loop from breaking when one of the values is 0:

    $ids  = array(0, 1, 2, 3, 4);
    $out = array();
    
    while ( !is_null( $item = array_shift($ids) )  ) {
        foreach ($ids as $key=>$value) {
            $out[] = array($item, $value);
        }
    
    }
    

提交回复
热议问题