PHP variables: references or copies - php

I'm confused about how PHP variable references work. In the examples below, I want to be able to access the string hello either as $bar[0] or $barstack[0][0]. It would seem that passing the array by reference in step 1 should be sufficient.
The second example does not work. $foostack[0]0] is the string hello, but $foo[0] doesn't exist. At some point, the first element of $foostack becomes a copy of $foo, instead of a reference.
The problem lies in the first line of step 2: When I push a reference on, I expect to pop a reference off. But array_pop returns a copy instead.
Others have told me that if I have to worry about references and copies, then PHP is not the right language for me. That might be the best answer I'm going to get.
FWIW, in order for var_dump to be useful, it needs to display some property that distinguishes between a reference and a copy. It does not. Maybe there's another function?
My first PHP project seems to be going badly. Can someone help shed some light on the
problems with this code?
<?php
echo "// This works!\n<br />" ;
// step 1
$bar = array() ;
$barstack = array( &$bar ) ;
// step 2
array_push( $barstack[0], 'hello' ) ;
// results
echo count( $barstack[0] ) .';' .count( $bar ) ;
echo "\n<br />// This doesn't :(\n<br />" ;
// step 1
$foo = array() ;
$foostack = array( &$foo ) ;
// step 2
$last = array_pop( $foostack ) ;
array_push( $last, 'hello' ) ;
array_push( $foostack, &$last ) ;
// results
echo count( $foostack[0] ) .';' .count( $foo ) ;
echo "\n<br />// Version:\n<br />" ;
echo phpversion() ."\n" ;
?>
The results can be viewed at the following URL:
http://www.gostorageone.com/tqis/hi.php
Version is 4.3.10. Upgrading the server is not practical.
Desired outcomes:
Explain the obvious if I've overlooked it
Is this a bug? Any workarounds?
Thanks!
-Jim

Your code works fine, there is no bug, and it is independent to PHP 4 or 5. Maybe it helps if this is simply explained to you.
Let's go through the example which does not work in your eyes, just looking what actually happens:
// step 1
$foo = array(); # 1.
$foostack = array( &$foo ); # 2.
1.: You initialize the variable $foo to an empty array.
2.: You initialize the variable $foostack to an array and the first element of the array is an alias of the variable $foo. This is exactly the same as writing: $foostack[] =& $foo;
On to the next step:
// step 2
$last = array_pop($foostack); # 3.
array_push($last, 'hello'); # 4.
array_push($foostack, &$last); # 5.
3.: You assign the last element's value of the array $foostack to $last and you remove the last element from the array $foostack. Note: array_pop returns a value, not a reference.
4.: You add 'hello' as a new element to an empty array in $last.
5.: You add &$last as a new element to $foostack;
So which variables do we have now?
First of all $foo which just contains an empty array. The last element of $foostack was once reference to it (2.), but you have removed that directly after (3.). As $foo and it's value has not been changed any longer, it's just an empty array array().
Then there is $last, which got an empty array in 3.. That's just an empty array, it's a value not a reference. In (4.) you add the string 'hello' as first element to it. $last is an array with one string element in there.
Then there is $foostack. It's an array that get's a reference to $foo in (2.), then that reference is removed in (3.). Finally an alias to $last is added to it.
This is exactly what the rest of your code outputs:
echo count($foostack[0]) .';'. count($foo);
$foostack[0] is the alias to $last - the array with the string 'hello' as only element, while $foo is just $foo, the empty array array().
It makes no difference if you execute that with PHP 4 or 5.
As you write that's "wrong", I assume you were just not able to achieve what you wanted. You're probably looking for a function that is able to return the reference to the last element of an array before removing it. Let's call that function array_pop_ref():
// step 1
$foo = array();
$foostack = array( &$foo );
// step 2
$last =& array_pop_ref($foostack);
array_push($last, 'hello');
array_push($foostack, &$last);
// results
echo count($foostack[0]) .';' .count($foo); # 1;1
The array_pop_ref function:
function &array_pop_ref(&$array)
{
$result = NULL;
if (!is_array($array)) return $result;
$keys = array_keys($array);
$end = end($keys);
if (false === $end) return $result;
$result =& $array[$end];
array_pop($array);
return $result;
}

Related

Indexing into an array returned by a variable variable in PHP

I'm just wondering about how variable variables that point to Arrays handle. Take the code below:
$a = new stdClass();
$a->b = array('start' => 2);
$c = 'b';
$d = 'start';
print $a->$c; // => Array
print $a->$c['start']; // => Array
print $a->$c[0]; // => Array
print $a->$c[0][0]; //=> PHP Fatal error: Cannot use string offset as an array in php shell code on line 1
The first print I expect, the second one I don't, or the 3rd. The 4th is expected after realizing that the evaluation of $a->$c is apparently a string. But then why does this work:
$t = $a->$c;
print $t['start']; //=> 2
I'm asking more because I'm curious than I need to know how to nicely do
$e = 'start';
$a->$c[$e]
Anyone know why I can't index directly into the array returned by the variable variable usage?
It comes down to order of operations and how PHP type juggles. Hint: var_dump is your friend, echo always casts to a string so it is the worst operation for analyzing variable values, esp in debug settings.
Consider the following:
var_dump($a->$c); // array (size=1) / 'start' => int 2
var_dump($a->$c['start']); // array (size=1) / 'start' => int 2
var_dump($a->b['start']); // int 2
var_dump($c['start']); // string 'b' (length=1)
The key here is how PHP interprets the part of $c['start'] (include $c[0] here as well). $c is the string 'b', and when attempting to get the 'start' index of string 'b' this simply returns the first character in the string, which happens to simply be the only letter (b) in the string. You can test this out by using $c = 'bcdefg'; - it'll yield the same result (in this specific case). Also, $c['bogus'] will yield the exact same thing as $c['start']; food for thought, and make sure you do the required reading I linked to.
So with this in mind (knowing that $c['start'] reluctantly returns 'b'), the expression $a->$c['start'] is interpreted at $a->b. That is, the order is $a->($c['start']) and not ($a->$c)['start'].
Unfortunately you can't use () nor {} to steer the parser (PHP SCREAMs), so you won't be able to accomplish what you want in a single line. The following will work:
$e = 'start';
$interim = $a->$c;
echo $interim[$e];
Alternatively, you can cast your arrays as objects (if you have the luxury):
$a->$c = (object) $a->$c; // mutate
var_dump($a->$c->$e);
$interim = (object) $a->$c; // clone
var_dump($interim->$e);
...by the way, referring back up to $c['start'] and $c[0], in regards to $c[0][0] you simply can't do this because $c[0] is the character b in string 'b'; when access the character/byte b it will not have a property of [0].
$a->$c[0]; is actually equal to: array('start' => 0)
so when you did:
print $a->$c[0][0];
You are trying to load an array element from $a->$c[0] at index 0 which does not exists.
however, this will work:
print $a->$c[0]['start'];

Getting element from PHP array returned by function

I'm not sure if this is possible, but I can't figure out how to do it if it is...
I want to get a specific element out of an array that is returned by a function, without first passing it into an array... like this...
$item = getSomeArray()[1];
function getSomeArray(){
$ret = Array();
$ret[0] = 0;
$ret[1] = 100;
return $ret;
}
What's the correct syntax for this?
PHP cannot do this yet, it's a well-known limitation. You have every right to be wondering "are you kidding me?". Sorry.
If you don't mind generating an E_STRICT warning you can always pull out the first and last elements of an array with reset and end respectively -- this means that you can also pull out any element since array_slice can arrange for the one you want to remain first or last, but that's perhaps taking things too far.
But despair not: the (at this time upcoming) PHP 5.4 release will include exactly this feature (under the name array dereferencing).
Update: I just thought of another technique which would work. There's probably good reason that I 've never used this or seen it used, but technically it does the job.
// To pull out Nth item of array:
list($item) = array_slice(getSomeArray(), N - 1, 1);
PHP 5.4 has added Array Dereferencing,
here's the example from PHP's Array documentation:
Example #7 Array dereferencing
function getArray() {
return ['a', 'b', 'c'];
}
// PHP 5.4
$secondElement = getArray()[0]; // output = a
// Previously
$tmp = getArray();
$secondElement = $tmp[0]; // output = a
The syntax you're referring to is known as function array dereferencing. It's not yet implemented and there's an RFC for it.
The generally accepted syntax is to assign the return value of the function to an array and access the value from that with the $array[1]syntax.
That said, however you could also pass the key you want as an argument to your function.
function getSomeArray( $key = null ) {
$array = array(
0 => "cheddar",
1 => "wensleydale"
);
return is_null($key) ? $array : isset( $array[$key] ) ? $array[$key] : false;
}
echo getSomeArray(0); // only key 0
echo getSomeArray(); // entire array
Yes, php can't do that. Bat you can use ArrayObect, like so:
$item = getSomeArray()->{1};
// Credits for curly braces for Bracketworks
function getSomeArray(){
$ret = Array();
$ret[0] = 0;
$ret[1] = 100;
return new ArrayObject($ret, ArrayObject::ARRAY_AS_PROPS);
}
Okay, maybe not working with numeric keys, but i'm not sure.
I know this is an old question, but, in your example, you could also use array_pop() to get the last element of the array, (or array_shift() to get the first).
<?php
$item = array_pop(getSomeArray());
function getSomeArray(){
$ret = Array();
$ret[0] = 0;
$ret[1] = 100;
return $ret;
}
As the others says, there is no direct way to do this, I usually just assign it to a variable like so:
$items = getSomeArray();
$item = $items[1];
function getSomeArray(){
$ret = Array();
$ret[0] = 0;
$ret[1] = 100;
return $ret;
}
I am fairly certain that is not possible to do in PHP. You have to assign the returned array to another array before you can access the elements within or use it directly in some other function that will iterate though the returned array (i.e. var_dump(getSomeArray()) ).

Peek ahead when iterating an array in PHP

Is it possible to "peek ahead" while iterating an array in PHP 5.2? For example, I often use foreach to manipulate data from an array:
foreach($array as $object) {
// do something
}
But I often need to peek at the next element while going through the array. I know I could use a for loop and reference the next item by it's index ($array[$i+1]), but it wouldn't work for associative arrays. Is there any elegant solution for my problem, perhaps involving SPL?
You can use the CachingIterator for this purpose.
Here is an example:
$collection = new CachingIterator(
new ArrayIterator(
array('Cat', 'Dog', 'Elephant', 'Tiger', 'Shark')));
The CachingIterator is always one step behind the inner iterator:
var_dump( $collection->current() ); // null
var_dump( $collection->getInnerIterator()->current() ); // Cat
Thus, when you do foreach over $collection, the current element of the inner ArrayIterator will be the next element already, allowing you to peek into it:
foreach($collection as $animal) {
echo "Current: $animal";
if($collection->hasNext()) {
echo " - Next:" . $collection->getInnerIterator()->current();
}
echo PHP_EOL;
}
Will output:
Current: Cat - Next:Dog
Current: Dog - Next:Elephant
Current: Elephant - Next:Tiger
Current: Tiger - Next:Shark
Current: Shark
For some reason I cannot explain, the CachingIterator will always try to convert the current element to string. If you want to iterate over an object collection and need to access properties an methods, pass CachingIterator::TOSTRING_USE_CURRENT as the second param to the constructor.
On a sidenote, the CachingIterator gets it's name from the ability to cache all the results it has iterated over so far. For this to work, you have to instantiate it with CachingIterator::FULL_CACHE and then you can fetch the cached results with getCache().
Use array_keys.
$keys = array_keys($array);
for ($i = 0; $i < count($keys); $i++) {
$cur = $array[$keys[$i]];
$next = $array[$keys[$i+1]];
}
You can use next and prev to iterate an array. current returns the current items value and key the current key.
So you could do something like this:
while (key($array) !== null) {
next($array); // set pointer to next element
if (key($array) === null) {
// end of array
} else {
$nextItem = current($array);
}
prev($array); // resetting the pointer to the current element
// …
next($array);
}
I know that this is an old post, but I can explain that current/next/prev thing better now.
Example:
$array = array(1,2,3,2,5);
foreach($array as $k => $v) {
// in foreach when looping the key() and current()
// is already pointing to the next record
// And now we can print current
print 'current key: '.$k.' and value: '.$v;
// if we have next we can print its information too (key+value)
if(current($array)) {
print ' - next key: '.key($array).' and value: '.current($array);
// at the end we must move pointer to next
next($array);
}
print '<br>';
}
// prints:
// current key: 0 and value: 1 - next key: 1 and value: 2
// current key: 1 and value: 2 - next key: 2 and value: 3
// current key: 2 and value: 3 - next key: 3 and value: 2
// current key: 3 and value: 2 - next key: 4 and value: 5
// current key: 4 and value: 5
I know I could use a for loop and reference the next item by its index ($array[$i+1]), but it wouldn't work for associative arrays.
Consider converting your associative array into an sequentially indexed one with array_values(), allowing you to use the simple for loop solution.
Old post but my two cents:
If you are trying to peek ahead, you really need to ask yourself "Am I solving this problem the best way possible."
You can solve all peek-ahead problems without ever doing a peek-ahead. All you need is a "$prevItem" reference declared before the collection and initialize it as null. Each time you go through the loop, at the end, set $prevItem to the current array item you just evaluated. Effectively, instead of peaking ahead, you start executing your real logic at the second item and use the $prevItem reference to do your operation. You skip the first item by noting that $prevItem is null.
$prevItem = null;
$prevKey = null;
foreach($collection as $key => $val)
{
if($prevItem != null)
{
//do your operation here
}
$prevItem = $val;
$prevKey = $key;
}
It's clean code and its a common pattern.
Stay away from poking around at underlying data structures while you are iterating through them... its never good practice, and extremely rare that you would need to do it.

each() and list() function

I don't understand the each() and the list() function that well. Can anyone please give me a little more detail and explain to me how can it be useful?
Edit:
<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>
Array
(
[1] => bob
[value] => bob
[0] => 0
[key] => 0
)
So does this mean in array[1] there's the value bob, but it's clearly in array[0]?
list is not a function per se, as it is used quite differently.
Say you have an array
$arr = array('Hello', 'World');
With list, you can quickly assign those different array members to variables
list($item1, $item2) = $arr; //$item1 equals 'Hello' and $item2 equals 'World'
The each() function returns the current element key and value, and moves the internal pointer forward.
each()
The list function — Assigns variables as if they were an array
list()
Example usage is for iteration through arrays
while(list($key,$val) = each($array))
{
echo "The Key is:$key \n";
echo "The Value is:$val \n";
}
A very common example for list is when thinking about CSV files. Imagine you have a simple database stored as CSV with the columns id, title and text, such a file could look like this:
1|Foo|Lorem ipsum dolor|
2|Bar|sit amet|
...
Now when you parse this file you could do it like this, using the list function:
$lines = file( 'myFile.csv' );
for ( $i = 0; $i < count( $lines ); $i++ )
{
list( $id, $title, $text, $null ) = explode( '|', $lines[$i], 4 );
echo "Id: $id, Title: $title\n$text\n\n";
}
The other function, each, is basically just an old way to walk through arrays, using internal pointers. A more common way to do that is by using foreach now.
Edit: its probably worth noting that each has been deprecated
Warning: This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged
lets look first at each() : it returns the current value (at first that's $array[0 | "first value in association array ] . so
$prices = ('x'=>100 , 'y'=>200 , 'z'= 300 );
say I wanna loop over these array without using a foreach loop .
while( $e = each($prices) ){
echo $e[key] . " " . $e[value] . "<br/>" ;
}
when each reaches point at a non existing element that would cause
the while loop to terminate .
When you call each(), it gives you an array with four values and the four indices to the array locations.The locations 'key' and 0 contain the key of the current element, and the locations 'value' and 1 contain the
value of the current element.
so this loop would list each key of the array a space and the value then
secondly lets look at list() .
it basically will do the same thing with renaming of 'value' and 'key' although it has to be use in conjunction with each()
while( list($k , $v ) = each($prices) ){
echo $k /*$e[key]*/ . " " . $v /*$e[value]*/ . "<br/>" ;
}
So in a nutshell each() iterates over the array each time returning an array . list() rename the value , key pairs of the array to be used inside the loop .
NOTICE : reset($prices) :
resets each() pointer for that array to be the first element .
http://www.php.net/list
list isn't a function, it is a language construct. It is used to assign multiple values to different variables.
list($a, $b, $c) = array(1, 2, 3);
Now $a is equal to 1, and so on.
http://www.php.net/each
Every array has an internal pointer that points to an element in its array. By default, it points to the beginning.
each returns the current key and value from the specified array, and then advances the pointer to the next value. So, put them together:
list($key, $val) = each($array);
The RHS is returning an array, which is assigned to $key and $val. The internal pointer in `$array' is moved to the next element.
Often you'll see that in a loop:
while(list($key, $val) = each($array)):
It's basically the same thing as:
foreach($array as $key => $val):
To answer the question in your first edit:
Basically, PHP is creating a hybrid array with the key/value pair from the current element in the source array.
So, you can get the key by using $bar[0] and the value by using $bar[1]. OR, you can get the key by using $bar['key'] and the value using $bar['value']. It's always a single key/value pair from the source array, it's just giving you two different avenues of accessing the actual key and actual value.
Say you have a multi-dimensional array:
+---+------+-------+
|ID | Name | Job |
| 1 | Al | Cop |
| 2 | Bob | Cook |
+---+------+-------+
You might do something like:
<?php
while(list($id,$name,$job) = each($array)) {
echo "".$name." is a ".$job;
}
?>
Using them together is, basically, an early way to iterate over associative arrays, especially if you didn't know the names of the array's keys.
Nowadays there's really no reason I know of not to just use foreach instead.
list() can be used separately from each() in order to assign an array's elements to more easily-readable variables.

Using arrays by reference

Why is the following code "crashing" in PHP?
$normal_array = array();
$array_of_arrayrefs = array( &$normal_array );
end( $array_of_arrayrefs )["one"] = 1; // choking on this one
The expected result is that the final code line appends $normal_array with key one having value 1 but there is no output what so ever, not even prints preceeding this code. In the real context of this scenario I use end() function to always append to the last array reference.
This doesn't crash, it just contains a syntax error:
end( $array_of_arrayrefs )["one"] = 1;
Unfortunately, you cannot treat function return values as arrays in PHP. You have to assign the value explicitly. Unfortunately, this doesn't work here because end makes a copy of the returned value.

Categories