-1

I am having one array with some elements and i want output where i can store each element in table as source and destination too. e.g

    $array = ['1','2','3,'4'];
     
    source | destination |
------------------------------
    1      | 2           |
    1      | 3           |
    1      | 4           |
    2      | 1           |
    2      | 3           |
    2      | 4           |
    3      | 1           |
    3      | 2           |
    3      | 4           |
    4      | 1           |
    4      | 2           |
    4      | 3           |

I want to store data like above structure. I tried with for each loop but not getting perfect logic for this.Please help me solve this.

4
  • 1
    Show us what you tried then at least.
    – C3roe
    Commented Feb 22, 2022 at 14:46
  • 2
    There is nothing wrong with asking for a bit of help with your homework. Beginners are welcome, but we expect a good faith attempt at an answer from you first. SO is not a free coding service although we are very willing to help you fix issues with code you have written. How do I ask and answer homework questions?
    – RiggsFolly
    Commented Feb 22, 2022 at 14:52
  • 1
    try a FOR loop inside a FOR loop
    – RiggsFolly
    Commented Feb 22, 2022 at 14:54
  • $array = ['1','2','3','4']; $firstEle = reset($array); // to get first element foreach($array as $val){ if($val != $firstEle){ $sql = "insert into table_name (source,destination) values ($firstEle, $val)"; } //able to enter for first element . } // by this i am able to store data for first element only. after that i am stuck, @CBroe
    – amit sutar
    Commented Feb 22, 2022 at 15:18

2 Answers 2

0

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"
  }
}
0
0

A loop in a loop with a quick check so you dont print when the indices are the same is all you need

$array = [1,2,3,4];

for ($i=0; $i<count($array); $i++){
    for ( $j=0; $j<count($array); $j++){
        if ( $i == $j ) {
            continue;
        }
        echo sprintf("%d - %d\n", $array[$i], $array[$j]);    
    }
}

RESULT

1 - 2
1 - 3
1 - 4
2 - 1
2 - 3
2 - 4
3 - 1
3 - 2
3 - 4
4 - 1
4 - 2
4 - 3
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.