English 中文(简体)
PHP 右、 右、 下、 左、 上在阵列中
原标题:PHP Matrix Peek Right, Down, Left, Up In The Arrays

我在 PHP 中有一个 3x3 矩阵:

2 4 6
5 4 7
8 5 7

假设我在 < code> [0,0] 元素上, 那么 2. 将我在多维阵列中的位置储存在可变美元流中的最佳方式是什么? 我的想法是指针, 但不确定如何在 PHP 中做到这一点 。

$current = $data[0][0];

不帮助, 因为它不存储上下文 。

第二,什么是最好的方法 来代号看右,下,左,上 假设美元流的存储?

谢谢

最佳回答

PHP 中没有指针, 您无法对引用进行指针算术 。

您可以使用 < a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\在\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

这些都是在单层深度上工作, 所以无法在一个多维阵列中找到位置 。

与多维阵列合作的最佳方式可能是简单地存储和递增/递减 X和Y 值,以识别数组中的位置。由于PHP 中的阵列是内部按hashes实施的,因此引用 $data[$x][$y] 的参考速度非常快,不管 $x $y 包含什么。

例如,如果您目前在矩阵中的位置是(1, 1),您可以有一个变量存储 :

$current = [ x  => 1,  y  => 1];

查一查那里储存了什么 查一查

$x = $current[ x ];
$y = $current[ y ];
$data[$x][$y];

要找出这个位置左上方的存储位置, 您可以简单地从当前位置坐标中减去 1, 然后查看该位置 :

$x = $current[ x ] - 1;
$y = $current[ y ] - 1;
$data[$x][$y];
问题回答

仅使用单值作为索引, 并且 value% 3 = 行 , (int)(value / 3) = 列 。 要向上减少 3, 向下增加 3, 向下增加 3, 左减少 1, 右增加 1, 还要确保您始终处于边界线 。

您能否将您目前的位置保存为二元素阵列?

$current = array(0,0)

然后您将访问位于 美元当量位置的变量, 使用 :

$array[$current[0]][$current[1]]

如果您正在对数组进行迭接 -- -- 我假设通过使用一对嵌套环环, 您只需在外环中递增 < code>$current[ 0] , 在内环中递增 $current[1] (并在外环中将其设置为零)。 请注意, 此选项将您的“ 垂直” 位置存放在 $urent 第一个元素中 。 如果您想要命令像标准 Cartesian 坐标一样 (x,y), 请将这两个元素倒转 。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签