PHP数组语法和操作说明
问题描述:
I have to convert a function from PHP to Java, and can use some clarification on PHP's array syntax, which has a few differences from Java. The PHP code is
$W; //array defined before and has values
$S = array();
$j = wsnum - 1; //integer value here
for ( ; ; ){
$S[] = $j
$S[] = $W[$j];
}
My interpretation of this snippet is
-
$S
is initialized as an array of length 0 -
$j
is pushed to$S[0]
- The contents of
$W[$j]
are pushed to$S[1]
Is my interpretation correct, or am I barking up the wrong tree?
我必须将函数从PHP转换为Java,并且可以对PHP的数组语法进行一些说明,它具有 与Java差异很小。 PHP代码是 p>
$ W; //之前定义的数组并且具有值
$ S = array();
$ j = wsnum - 1; //这里的整数值
for(;;){
$ S [] = $ j
$ S [] = $ W [$ j];
}
code> pre>
我对此代码段的解释是 p>
-
$ S 初始化为长度为0的数组 li>
-
$ j code>被推送到 $ S [0] code> li>
-
$ W [$ j] code>的内容被推送到 $ S [1] code> li>
ol>
我的解释是正确的,还是我在错误的树上咆哮? p>
div>
答
Your interpretation is totally correct.
You can look at this post to learn more about php operators.
The []
operator is a "push" operator. Which always put the value assigned at the end of a given array.