php - Combining array values in pairs without pairing with previous elements in array -
i'm having trouble combining array values. have tried combine them , make pairs have done want erase pairs not want. please 1 me happen .
// code have is: $inputarray = array('mussafiri', 'fire', 'ubungo', 'mbezi'); $outputarray = array(); $i = 0; foreach($inputarray $values) { $j = 0; foreach($inputarray $values2) { if($values != $values2){ $outputarray[] = array($values => $values2); } $j++; } $i++; } print_r($outputarray); //output array is: array ( [0] => array ( [mussafiri] => fire) [1] => array ( [mussafiri] => ubungo) [2] => array ( [mussafiri] => mbezi) [3] => array ( [fire] => mussafiri) [4] => array ( [fire] => ubungo) [5] => array ( [fire] => mbezi) [6] => array ( [ubungo] => mussafiri) [7] => array ( [ubungo] => fire) [8] => array ( [ubungo] => mbezi) [9] => array ( [mbezi] => mussafiri ) [10] => array ( [mbezi] => fire) [11] => array ( [mbezi] => ubungo) ) note: not want following pairs appear, want element pair next element , not pair previous element not index 3,6,7,8,9,10 , 11 appear in pair list i.e [3] => array ( [fire] => mussafiri) [6] => array ( [ubungo] => mussafiri) [7] => array ( [ubungo] => fire) [8] => array ( [ubungo] => mbezi) [9] => array ( [mbezi] => mussafiri) [10] => array ( [mbezi] => fire) [11] => array ( [mbezi] => ubungo)
if understand correctly you're trying create unique pairs array, in case should work:
$pairs = array(); foreach ($inputarray $key => $value) { ($i = $key + 1; isset($inputarray[$i]); $i++) { $pairs[] = array($value => $inputarray[$i]); } }
the loop sets $i
key of current array element + 1, it'll never referencing itself, , never reference previous array members.
Comments
Post a Comment