Linked Questions
37 questions linked to/from Are arrays in PHP copied as value or as reference to new variables, and when passed to functions?
14
votes
3
answers
25k
views
php array assign by copying value or by reference? [duplicate]
Possible Duplicate:
are arrays in php passed by value or by reference?
I heard that PHP can select how to assign arrays, depends on array size.
It can assign by copying value (as any scalar type) ...
0
votes
2
answers
6k
views
Arrays passed by reference or by value [duplicate]
Possible Duplicate:
Are arrays or lists passed by default by reference in c#?
I have read some contradicting posts on here about C# and whether arrays are passed by value or by reference. Could ...
-2
votes
1
answer
124
views
Why can't I append an element in a PHP class' nested array? [duplicate]
I have a PHP class containing a 2-dimensional array:
class ArrApp
{
private $cms = [
'S' => 'A',
'V' => []
...
-2
votes
1
answer
140
views
Class array property by reference [duplicate]
If I have the following variable in a class
protected $address
and in a function test, I do
$testAddress = $this->address;
and then
$testAddress['state'] = 'KO';
Does that change the state for $...
0
votes
1
answer
71
views
Why does this reference passing not work as expected? [duplicate]
Consider the following code:
class FOO {
public $v = [];
function &refv1() {
return $this->v[1];
}
function refv2(&$ref) {
$ref = &$this->v[2];
}
}
$FOO = new FOO(...
7743
votes
87
answers
2.8m
views
Is Java "pass-by-reference" or "pass-by-value"?
I always thought Java uses pass-by-reference. However, I read a blog post which claims that Java uses pass-by-value. I don't think I understand the distinction the author is making.
What is the ...
316
votes
16
answers
233k
views
Are PHP Variables passed by value or by reference?
Are PHP variables passed by value or by reference?
21
votes
4
answers
29k
views
Get second to last value in array
I'm frequently using the following to get the second to last value in an array:
$z=array_pop(array_slice($array,-2,1));
Am I missing a php function to do that in one go or is that the best I have?
11
votes
1
answer
15k
views
PHP: Return a reference to an array element
Is there a way in PHP to return a reference to an element in array?
function ref(&$array, &$ref) { $ref = $array[1]; }
$array = array(00, 11, 22, 33, 44, 55, 66, 77, 88, 99);
ref($array, $ref)...
3
votes
2
answers
14k
views
php: how to update a value in associative array by key
I'm having a hard time trying to update the values in my array. I made a simple example to illustrate this: the array contains names of players and the amount of points they have. After each round I ...
6
votes
4
answers
852
views
Convert JavaScript function to PHP
I have a JS function, with array inputs.For example:
x=[ 239709880, 250229420, 109667654, 196414465, 13098 ]
y=[ 78135241, 54642792, 249 ]
OR:
x=[ 0, 0, 0, 0, 0, 0, 1 ]
y=[ 78135241, 54642792, 249 ]...
9
votes
3
answers
2k
views
When is foreach with a parameter by reference dangerous?
I knew, that it can be dangerous to pass the items by reference in foreach.
In particular, one must not reuse the variable that was passed by reference, because it affects the $array, like in this ...
0
votes
2
answers
2k
views
Set variable in foreach loop and pass by reference
I was playing with PHP recently and wanted to assign variable in foreach loop and pass value by reference at the same time. I was a little bit surprised that didn't work. Code:
$arr = array(
'one'...
1
vote
5
answers
436
views
How to check whether one array is a correctly ordered subset of another
I have a few arrays of strings like:
$big = ['html', 'body', 'div', 'table', 'tbody', 'tr', 'td'];
$small = ['body', 'div', 'td'];
$wrong = ['td', 'body', 'div'];
I need to check whether $small and $...
-4
votes
1
answer
1k
views
Are arrays passed by reference or value in PHP?
Are arrays passed by reference or value in PHP?
For example, let's see this code.
function addWeight($arout, $lineCountry, $lineDomain, $target, $weight)
{
$currentDomain=getDomain();
$...