在php中创建二维矩阵

发布于 2024-08-14 11:56:15 字数 837 浏览 2 评论 0原文

问题是我必须在 php 中创建一个 2D 矩阵,其中每一行和每一列都必须有一个键。我尝试这样做,但发生的情况是创建了一个与矩阵不同的二维数组。我使用了以下代码:

<代码> $x=$row['start_id'];
$y=$row['dest_id'];
$d=$row['距离'];
$this->map[$x][$y]=$d;

这里的地图是预期的矩阵。此代码的目的是创建一个邻接矩阵,然后用最大距离填充未设置的单元格。上面代码中的 $x、$y 和 $d 来自 mysql 查询的结果。

示例输出:

Array (
    [10010012] => Array ( 
        [10010013] => 2
        [10010016] => 8 
    )
    [10010016] => Array ( 
        [10010015] => 5 
    )
    [10010013] => Array ( 
        [10010014] => 7 
        [10010016] => 3
    )
    [10010014] => Array ( 
        [10010015] => 2 
    )
)

现在的问题是我无法填充空单元格
例如行键=>[10010012]和列键=>[10010015](无法设置值)

任何帮助表示赞赏。如果可能的话,还提到如何遍历这样的矩阵。

我是一个相对初学者,并已尽力解释我的问题。但如果您发现任何不足之处,请指出。

编辑:矩阵不是方形的。

The thing is I have to create a 2D matrix in php where each row and column must have a key. I tried to do this but what happens is that a 2-D array is created which does not resemble a matrix. I used the following code:


$x=$row['start_id'];
$y=$row['dest_id'];
$d=$row['distance'];
$this->map[$x][$y]=$d;

Here map is the intended matrix. The intention of this code is to create an adjacency matrix and then fill the unset cells with maximum distance. $x, $y and $d in above code are derived from result of a mysql query.

Sample Output:

Array (
    [10010012] => Array ( 
        [10010013] => 2
        [10010016] => 8 
    )
    [10010016] => Array ( 
        [10010015] => 5 
    )
    [10010013] => Array ( 
        [10010014] => 7 
        [10010016] => 3
    )
    [10010014] => Array ( 
        [10010015] => 2 
    )
)

Now the problem is that I am not able to fill the empty cells

e.g. row key =>[10010012] and column key=>[10010015] (Not able to set value)

Any help is appreciated. If possible also mention how to traverse through such matrices.

I am a relative beginner and have tried my best to explain my problem. However if you find any shortcomings please point them out.

Edit: The matrix is not a square one.

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

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

发布评论

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

评论(1

网名女生简单气质 2024-08-21 11:56:15

另一方面

$this->map[10010012][10010015]= MAX_DISTANCE;

,为什么要将所有空/不存在的单元格设置为 MAX_DISTANCE?您可以让地图不完整,只要单元格不存在,您就假定 MAX_DISTANCE 作为其值。

编辑:简单的示例

define('MAX_DISTANCE', PHP_INT_MAX);

$map = array(
  10010012 => array ( 10010013 => 2, 10010016 => 8),
  10010016 => array ( 10010015 => 5 ),
  10010013 => array ( 10010014 => 7, 10010016 => 3),
  10010014 => array ( 10010015 => 2 )
);

function getValue(&$map, $x, $y) {
  return isset($map[$x][$y]) ? $map[$x][$y] : MAX_DISTANCE;
}

function setValue(&$map, $x, $y, $value) {
  if ( !isset($map[$x]) ) {
    $map[$x] = array($y => $value);
  }
  else {
    $map[$x][$y] = $value;
  }
}

// get an "existing" value from $map
echo getValue($map, 10010012, 10010016), "\n";
// get a "non-existing" value from $map
echo getValue($map, 10010014, 10010016), "\n";

// set a "new" value
setValue($map, 10010014, 10010016, 5);
// $map has been altered
var_dump($map[10010014]);

打印

8
2147483647
array(2) {
  [10010015]=>
  int(2)
  [10010016]=>
  int(5)
}

That would be

$this->map[10010012][10010015]= MAX_DISTANCE;

On the other hand, why do you want to set all empty/non-existing cell to MAX_DISTANCE? You can leave the map incomplete and whenever a cell does not exist you assume MAX_DISTANCE as its value.

edit: simple example

define('MAX_DISTANCE', PHP_INT_MAX);

$map = array(
  10010012 => array ( 10010013 => 2, 10010016 => 8),
  10010016 => array ( 10010015 => 5 ),
  10010013 => array ( 10010014 => 7, 10010016 => 3),
  10010014 => array ( 10010015 => 2 )
);

function getValue(&$map, $x, $y) {
  return isset($map[$x][$y]) ? $map[$x][$y] : MAX_DISTANCE;
}

function setValue(&$map, $x, $y, $value) {
  if ( !isset($map[$x]) ) {
    $map[$x] = array($y => $value);
  }
  else {
    $map[$x][$y] = $value;
  }
}

// get an "existing" value from $map
echo getValue($map, 10010012, 10010016), "\n";
// get a "non-existing" value from $map
echo getValue($map, 10010014, 10010016), "\n";

// set a "new" value
setValue($map, 10010014, 10010016, 5);
// $map has been altered
var_dump($map[10010014]);

prints

8
2147483647
array(2) {
  [10010015]=>
  int(2)
  [10010016]=>
  int(5)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文