Home > front end >  How to store each element as source and destination from array in php?
How to store each element as source and destination from array in php?

Time:02-23

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.

CodePudding user response:

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

CodePudding user response:

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
  • Related