Is it possible to see if two array variables point to the same memory location? (they are the same array)
Answers
Yes, it is possible. Here's an old article that I used when I wrote my first extension:
http://web.archive.org/web/20110222035803/http://devzone.zend.com/article/1021
Things may have changed since then, so you may want to search for similar tutorials for additional references.
Oh, and this should be useful:
http://www.php.net/manual/en/internals2.php
This should get you started in the right direction
$ar3 = array('Nov'=>'-', 'Sept'=>'-', ...);
for($i = 0; $i < count($ar1); $i++){
$ar3[$ar2[$i]] = $ar1[$i];
}
Because the arrays are multidimensional you have to extract the ids like this:
$ids1 = array();
foreach($array1 as $elem1)
$ids1[] = $elem1['id'];
$ids2 = array();
foreach($array2 as $elem2)
$ids2[] = $elem2['id'];
$one_not_two = array_diff($ids1,$ids2);
For your specific question, check out array_diff() with multidimensional arrays
The simplest way I know:
$a == $b;
Note that you can also use the ===
. The difference between them is:
With Double equals
==
, order is important:$a = array(0 => 'a', 1 => 'b'); $b = array(1 => 'b', 0 => 'a'); var_dump($a == $b); // true var_dump($a === $b); // false
With Triple equals
===
, types matter:$a = array(0, 1); $b = array('0', '1'); var_dump($a == $b); // true var_dump($a === $b); // false
Reference: Array operators
Actually, this can be done. Through a php extension.
File: config.m4
File: php_test.h
File: test.c
Then all you have to do is phpize it, config it, and make it. Add a "extension=/path/to/so/file/modules/test.so" to your php.ini file. And finally, restart the web server, just in case.
Returns(at least for me, your memory addresses will probably be different)
Thanks to Artefacto for pointing this out, but my original code was passing the arrays by value, so thereby was recreating arrays including the referenced-one, and giving you bad memory values. I have since changed the code to force all params to be passed by reference. This will allow references, arrays, and object, to be passed in unmolested by the php engine. $w/$z are the same thing, but $w/$x/$y are not. The old code, actually showed the reference breakage and the fact that the memory addresses would change or match when all variables were passed in vs multiple calls to the same function. This was because PHP would reuse the same memory when doing multiple calls. Comparing the results of the original function would be useless. The new code should fix this problem.
FYI - I'm using php 5.3.2.