Split array into unique pairs

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

    Fixed from my initial jump-the-gun suggestion of array_chunk()

    Try this instead:

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

提交回复
热议问题