如何在PHP中的数组内使用数组引用?

时间:2022-04-03 09:02:31

I want to be able to do the following:

我希望能够做到以下几点:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

// Here I want to access the $normal_array reference **as a reference**,
// but that doesn't work obviously. How to do it?
end( $array_of_arrayrefs )["one"] = 1; // choking on this one

print $normal_array["one"]; // should output 1

Regards

/R

5 个解决方案

#1


4  

end() doesn't return a reference of the last value, but rather the last value itself. Here is a workaround:

end()不返回最后一个值的引用,而是返回最后一个值本身。这是一个解决方法:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

$refArray = &end_byref( $array_of_arrayrefs );
$refArray["one"] = 1;

print $normal_array["one"]; // should output 1

function &end_byref( &$array ) {
    $lastKey = end(array_keys($array));
    end($array);
    return $array[$lastKey];
}

#2


1  

Here are a couple of approaches, neither of which I find particularly satisfying. I'm sure there's a better way..

这里有几种方法,我觉得这两种方法都不是特别令人满意。我相信有更好的方法..

<?php
$normal_array       = array();
$array_of_arrayrefs = array( "blah", &$normal_array );

foreach ($array_of_arrayrefs as &$v);
$v["one"] = 1;

echo $normal_array["one"];  //prints 1
?>


<?php
$normal_array       = array();
$array_of_arrayrefs = array( "blah", &$normal_array );

$lastIndex = @end(array_keys($array_of_arrayrefs)); //raises E_STRICT because end() expects referable.
$array_of_arrayrefs[$lastIndex]["one"] = 1;

echo $normal_array["one"];  //prints 1
?>

#3


1  

You probably shouldn't be passing by reference in the first place. It's generally considered bad practise to do so, because it makes it hard to see where state gets modified.

你可能不应该首先通过引用传递。这通常被认为是不好的做法,因为它很难看出状态被修改的地方。

It's a very common misconception that references are faster. This is not the case - In fact, they are a little bit slower, but it's by such a small amount, that it really doesn't matter. PHP has a system called copy-on-write, which means that variables aren't actually copied, before you write to them.

这是一个非常常见的误解,即引用速度更快。事实并非如此 - 事实上,它们有点慢,但它的数量很少,它确实无关紧要。 PHP有一个名为copy-on-write的系统,这意味着在写入变量之前,实际上并没有复制变量。

The only place where you really need references, were in PHP4, where objects would get cloned otherwise. This is not needed in PHP5.

唯一真正需要引用的地方是PHP4,否则会克隆对象。 PHP5中不需​​要这样做。

#4


0  

The function end() doesn't just return a value. It also moves the array's internal pointer. Then we can use key() to get the index, after which we're able to use regular array access for the assignment.

函数end()不仅返回一个值。它还移动数组的内部指针。然后我们可以使用key()来获取索引,之后我们就可以使用常规数组访问来进行赋值。

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

end($array_of_arrayrefs);
$array_of_arrayrefs[ key($array_of_arrayrefs) ]["one"] = 1;

print $normal_array["one"];

#5


-1  

The line:

end( $array_of_arrayrefs )["one"] = 1; // choking on this one

结束($ array_of_arrayrefs)[“one”] = 1; //窒息这个

throws a parse error:

抛出一个解析错误:

Parse error: syntax error, unexpected '[' in /file.php on line 65

解析错误:语法错误,第65行的/file.php中的意外'['

Make sure you have error_reporting and display_error activated.

确保已激活error_reporting和display_error。

I'm not sure what you want to do but this works:

我不确定你想做什么,但这有效:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );
// Here I want to access the $normal_array reference **as a reference**,
// but that doesn't work obviously. How to do it?
$array_of_arrayrefs[0]["one"] = 1;
//end($array_of_arrayrefs )["one"] = 1; // choking on this one
print $normal_array["one"]; // should output 1

#1


4  

end() doesn't return a reference of the last value, but rather the last value itself. Here is a workaround:

end()不返回最后一个值的引用,而是返回最后一个值本身。这是一个解决方法:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

$refArray = &end_byref( $array_of_arrayrefs );
$refArray["one"] = 1;

print $normal_array["one"]; // should output 1

function &end_byref( &$array ) {
    $lastKey = end(array_keys($array));
    end($array);
    return $array[$lastKey];
}

#2


1  

Here are a couple of approaches, neither of which I find particularly satisfying. I'm sure there's a better way..

这里有几种方法,我觉得这两种方法都不是特别令人满意。我相信有更好的方法..

<?php
$normal_array       = array();
$array_of_arrayrefs = array( "blah", &$normal_array );

foreach ($array_of_arrayrefs as &$v);
$v["one"] = 1;

echo $normal_array["one"];  //prints 1
?>


<?php
$normal_array       = array();
$array_of_arrayrefs = array( "blah", &$normal_array );

$lastIndex = @end(array_keys($array_of_arrayrefs)); //raises E_STRICT because end() expects referable.
$array_of_arrayrefs[$lastIndex]["one"] = 1;

echo $normal_array["one"];  //prints 1
?>

#3


1  

You probably shouldn't be passing by reference in the first place. It's generally considered bad practise to do so, because it makes it hard to see where state gets modified.

你可能不应该首先通过引用传递。这通常被认为是不好的做法,因为它很难看出状态被修改的地方。

It's a very common misconception that references are faster. This is not the case - In fact, they are a little bit slower, but it's by such a small amount, that it really doesn't matter. PHP has a system called copy-on-write, which means that variables aren't actually copied, before you write to them.

这是一个非常常见的误解,即引用速度更快。事实并非如此 - 事实上,它们有点慢,但它的数量很少,它确实无关紧要。 PHP有一个名为copy-on-write的系统,这意味着在写入变量之前,实际上并没有复制变量。

The only place where you really need references, were in PHP4, where objects would get cloned otherwise. This is not needed in PHP5.

唯一真正需要引用的地方是PHP4,否则会克隆对象。 PHP5中不需​​要这样做。

#4


0  

The function end() doesn't just return a value. It also moves the array's internal pointer. Then we can use key() to get the index, after which we're able to use regular array access for the assignment.

函数end()不仅返回一个值。它还移动数组的内部指针。然后我们可以使用key()来获取索引,之后我们就可以使用常规数组访问来进行赋值。

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );

end($array_of_arrayrefs);
$array_of_arrayrefs[ key($array_of_arrayrefs) ]["one"] = 1;

print $normal_array["one"];

#5


-1  

The line:

end( $array_of_arrayrefs )["one"] = 1; // choking on this one

结束($ array_of_arrayrefs)[“one”] = 1; //窒息这个

throws a parse error:

抛出一个解析错误:

Parse error: syntax error, unexpected '[' in /file.php on line 65

解析错误:语法错误,第65行的/file.php中的意外'['

Make sure you have error_reporting and display_error activated.

确保已激活error_reporting和display_error。

I'm not sure what you want to do but this works:

我不确定你想做什么,但这有效:

$normal_array       = array();
$array_of_arrayrefs = array( &$normal_array );
// Here I want to access the $normal_array reference **as a reference**,
// but that doesn't work obviously. How to do it?
$array_of_arrayrefs[0]["one"] = 1;
//end($array_of_arrayrefs )["one"] = 1; // choking on this one
print $normal_array["one"]; // should output 1