With foreach you need to loop through the array twice, and if element in the first loop equals the element from the second loop you skip it, else you add first element as source and secound as destination. Something like this:
$array = ['1','2','3','4'];
$newArr = [];
foreach ($array as $a) {
foreach ($array as $b) {
if ($a == $b) {
continue;
}
$newArr[] = [
'source' => $a,
'data' => $b,
];
}
}
echo "<pre>";
var_dump($newArr);
echo "</pre>";
Result is:
array(12) {
[0]=>
array(2) {
["source"]=>
string(1) "1"
["destination"]=>
string(1) "2"
}
[1]=>
array(2) {
["source"]=>
string(1) "1"
["destination"]=>
string(1) "3"
}
[2]=>
array(2) {
["source"]=>
string(1) "1"
["destination"]=>
string(1) "4"
}
[3]=>
array(2) {
["source"]=>
string(1) "2"
["destination"]=>
string(1) "1"
}
[4]=>
array(2) {
["source"]=>
string(1) "2"
["destination"]=>
string(1) "3"
}
[5]=>
array(2) {
["source"]=>
string(1) "2"
["destination"]=>
string(1) "4"
}
[6]=>
array(2) {
["source"]=>
string(1) "3"
["destination"]=>
string(1) "1"
}
[7]=>
array(2) {
["source"]=>
string(1) "3"
["destination"]=>
string(1) "2"
}
[8]=>
array(2) {
["source"]=>
string(1) "3"
["destination"]=>
string(1) "4"
}
[9]=>
array(2) {
["source"]=>
string(1) "4"
["destination"]=>
string(1) "1"
}
[10]=>
array(2) {
["source"]=>
string(1) "4"
["destination"]=>
string(1) "2"
}
[11]=>
array(2) {
["source"]=>
string(1) "4"
["destination"]=>
string(1) "3"
}
}