Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php数组元素是通过引用访问的吗?_Php_Arrays_Pass By Reference - Fatal编程技术网

php数组元素是通过引用访问的吗?

php数组元素是通过引用访问的吗?,php,arrays,pass-by-reference,Php,Arrays,Pass By Reference,问题的标题可能很难解释,下面的代码可能会有所帮助 $containers = array(); // array of arrays for ($index = 0; $index < 4; $index++) { $containers[] = array(); // each element of the array is an array } foreach ($objects as $object) { $index = computeIndex($object); //

问题的标题可能很难解释,下面的代码可能会有所帮助

$containers = array(); // array of arrays
for ($index = 0; $index < 4; $index++) {
  $containers[] = array(); // each element of the array is an array
}

foreach ($objects as $object) {
  $index = computeIndex($object); // compute the index into the $containers
  $container = $containers[$index]; // get the subarray object
  $container[] = $object; // append $object to the end of the subarray
  $containers[$index] = $container; // <--- question: is this needed?
}
$containers=array();//数组的数组
对于($index=0;$index<4;$index++){
$containers[]=array();//数组的每个元素都是一个数组
}
foreach($objects作为$object){
$index=computeIndex($object);//将索引计算到$containers中
$container=$containers[$index];//获取子数组对象
$container[]=$object;//将$object追加到子数组的末尾

$containers[$index]=$container;//是需要最后一行;数组元素存储为值而不是引用。但是,PHP允许您使用以下内容创建引用:

您还可以省去一些麻烦,只需执行以下操作:

$containers[$index][] = $object;

太棒了!我还需要初始化$containers的第一个循环吗?是的,但是你可以使用array\u fill来代替
$containers[$index][] = $object;