Heim  >  Artikel  >  Backend-Entwicklung  >  请问 关于字数组的问题

请问 关于字数组的问题

WBOY
WBOYOriginal
2016-06-23 13:31:54762Durchsuche

请问


$test = array();
function abc($port, $index = 0){
if (!isset($GLOBALS['test'][$index]))
$GLOBALS['test'][$index] = array();
$GLOBALS['test'][$index]  [] = $port;
}

?>

请问 以上代码中  而原先在函数外定义test是一维数组的,
但是$GLOBALS['test'][$index]  [] = $port; 这样test就变成了  2维数组了。 
那么  能不能理解为  这个数组的 一行中有多少个数据 有   $port决定,即 $port如果是3,那么这个
数组的一行就是有三个数据。  是这样吗?  麻烦会的朋友帮忙回复下,谢啦


回复讨论(解决方案)

php 的数组不是传统意义上的数组
只在是二维数组时,勉强可附会行和列的概念

php 的数组不是传统意义上的数组
只在是二维数组时,勉强可附会行和列的概念



谢谢  xuzuning 版主的回复

还想请问下  那么 $GLOBALS['test'][$index] [] = $port;  这句话应该怎么理解呢?

$GLOBALS['test'][$index] [] = $port;
等于
array_push($GLOBALS['test'][$index],$port);

$GLOBALS['test'][$index] [] = $port;
等于
array_push($GLOBALS['test'][$index],$port);



谢谢  fdipzone 版主的回复

但是我还有些地方不太明白。

比如说    $port = 1;
那么当运行 $GLOBALS['test'][$index] [] = $port;的时候, 那么这时候  $GLOBALS['test'][$index] []  里边的
$GLOBALS['test'][$index]  [0]  为1 ,是这样吗?。也就是说 它就是2维数组。 或者说 能理解为 它是2为数组吗。


$GLOBALS['test'][$index] [] = $port;
等于
array_push($GLOBALS['test'][$index],$port);



谢谢  fdipzone 版主的回复

但是我还有些地方不太明白。

比如说    $port = 1;
那么当运行 $GLOBALS['test'][$index] [] = $port;的时候, 那么这时候  $GLOBALS['test'][$index] []  里边的
$GLOBALS['test'][$index]  [0]  为1 ,是这样吗?。也就是说 它就是2维数组。 或者说 能理解为 它是2为数组吗。


是的,你print_r 打印出来看看就知道了。
$GLOBALS['test'][$index] [0]  其实这样已经是多维数足组了。

真有本事,非要把简单的事情弄复杂了

$test = array();
$port = 1;

$GLOBALS['test'][$index] [] = $port;
将 $port 追加到数组 $GLOBALS['test'][$index] 中
即 将 $port 追加到数组 $test[$index] 中
完成后
$GLOBALS['test'] 是3维数组
$test 是2维数组

之所以说 php 的 array 不是传统意义的数组,是因为数组是齐次的,即每行的列数相同
而 php 的 array 就没有这个约束 array(array(1,2,3), array(4))
另外标记数组元素位置的值连续数字,而 php 的 array 也没有这个约束 array(3 => array(1,2,3), 8 => array(4))
还可以使用名称(关联键)标记 array('c' => array(1,2,3), array(4))

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn