[英]PHP Array & Pointer Confusion
Let's say I have a function like this: 假设我有一个像这样的函数:
function z($zzz){
for($c=0;$c<5;$c++){
$zzz[$c]= 10;
//more and more codes
}
}
I want to write a loop
so that 我想写一个
loop
这样
the 1st time the function is executed, argument $array
is passed 第一次执行函数时, 传递参数
$array
the 2nd time : argument $array[0]
is passed 第二次: 参数
$array[0]
被传递
while the 3rd time : argument $array[1]
is passed 第三次: 参数
$array[1]
被传递
..... .....
and the 12th time : argument $array[0][0]
is passed 第十二次: 参数
$array[0][0]
被传递
This is what comes to my mind: 这就是我的想法:
$a = -1;
$b = -1;
$array = array();
while($a<10){
while($b<10){
z($array);
$b++;
$array= &$array[$b];
}
$a++;
$array= &$array[$a];
}
I've tried it but it didn't work.. 我已经尝试过了,但是没有用..
I would appreciate if someone can provide a solution.. 如果有人可以提供解决方案,我将不胜感激。
如果z()应该更改传递的数组,则函数定义应为:
function z(&$zzz)
$a = 0;
while ($a < 99) // <-- edit as applicable
{
$b = 0
while ($b < 12)
{
if ($b == 0)
{
Z($array[$a]);
} else
{
Z($array[$a][$b]);
}
$b++;
}
$a++;
}
And as stated by Jack you need to pass the $array
variable by reference for update. 正如Jack所说,您需要通过引用传递
$array
变量以进行更新。 Not too sure what that function is trying to achieve though. 不太确定该功能试图实现什么。 If you need to fill an array with a predermined dimension, maybe
array_fill
could be more useful. 如果您需要使用预先确定的维度填充数组,也许
array_fill
可能会更有用。
http://www.php.net/manual/en/function.array-fill.php http://www.php.net/manual/zh/function.array-fill.php
function z(&$zzz){
for($c=0;$c<5;$c++){
$zzz[$c]= 10;
//more and more codes
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.