如何在php中将数组中的每个元素存储为源和目标?

发布于 2025-01-09 02:37:38 字数 547 浏览 0 评论 0原文

我有一个包含一些元素的数组,我想要输出,我也可以将表中的每个元素存储为源和目标。 例如

    $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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光与爱终年不遇 2025-01-16 02:37:38

使用 foreach 您需要循环遍历数组两次,如果第一个循环中的元素等于第二个循环中的元素,则跳过它,否则将第一个元素添加为源,将第二个元素添加为目标。像这样的事情:

 $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>";

结果是:

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

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"
  }
}
长安忆 2025-01-16 02:37:38

您只需要一个循环中的循环和快速检查,这样当索引相同时就不会打印

$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]);    
    }
}

结果

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文