|
Subject:
|
Remove item from an array?
|
|
Posted By:
|
jefferis
|
Post Date:
|
11/26/2003 3:10:52 PM
|
I have an array (photo, design, services, contact) and I need to issue a command to every item in the array except the currently selected item. I may have to create a temporary new array from the remaining items, but I'm not sure if that is needed or possible. I'm trying to use this for a Flash production, so the syntax is a bit different but the concept is the same. Each item in the array represents a graphic page that can be hidden. On selecting a button, the variable $currentItem is defined and replaced each time a new navigation button is selected. However, in order to set the value of the previous page to a hidden state, I have to issue a command to each item in the array. The problem is that such a command would also hide the current selected page. I've been struggling with this for a week and a half and can't find a way to selectively remove an item from an array. I know you can remove from the beginning or end, but don't see a select remove.
I'd rather be drawing...
|
|
Reply By:
|
nikolai
|
Reply Date:
|
11/26/2003 4:20:27 PM
|
It's actually not so difficult to remove an item from an array:
unset ($array[<target index>]);
Also, you don't even have to remove the item -- you can process each item in the array EXCEPT the one you want to skip. This is less efficient, since you have to check each element being processed whether it's the one you want to skip or not, but this same item-by-item comparison is pretty much what's involved in finding (and removing) an array.
function process_items(&$value, $key, $skip_key) { if($key != $skip_key) { $value['hidden'] = true; // or whatever it is you do } }
array_walk($your_array, 'process_items', 'the key to skip');
http://www.php.net/unset http://www.php.net/array_walk
Take care,
Nik http://www.bigaction.org/
|
|
Reply By:
|
jefferis
|
Reply Date:
|
11/26/2003 5:48:49 PM
|
Thanks Nik. Just a couple of follow ups. unset ($array[<target index>]);
With the target index, are you saying it has to be the array numeric value [0,1,2] etc, or can it be the key name?
unction process_items(&$value, And why is the & necessary in this ?
Thanks Jeff
I'd rather be drawing...
|
|
Reply By:
|
nikolai
|
Reply Date:
|
11/26/2003 7:18:32 PM
|
It MUST be the actual array key. Suppose you have the following array:
$p2p_posters = array("nikolai" => "USA", "jefferis" => "USA");
unset($p2p_posters[0]) won't do anything, because $p2p_posters[0] doesn't exist.
The ampersand (&) in front of the $value parameter means that you're accepting a REFERENCE to the parameter.
The default parameter passing mechanism in PHP is to pass copies of values around, so any modifications to those items ONLY modify the copy of the variable local to the scope of the called function.
By passing the item by reference, you're really saying "this variable is an alias for this other piece of data defined in some other scope". By "piece of data", I could mean a variable of any type, or the value stored in an array.
Quick example:
function change_value($var) { $var = 12345; }
function change_reference(&$var) { $var = "Look at this!!"; }
$foo = 5;
echo "initial value: $foo"; // prints "initial value: 5"
change_value($foo); echo "after change_value(): $foo"; // prints "after change_value(): 5"
change_reference($foo); echo "after change_reference(): $foo"; // prints "after change_reference(): Look at this!!"
For more info, read up on references in the manual: http://www.php.net/references
Take care,
Nik http://www.bigaction.org/
|
|
Reply By:
|
jefferis
|
Reply Date:
|
11/27/2003 10:29:02 AM
|
Hi Nik,
Well, I'm having trouble translating this into Actionscript because it lacks both unset and array_walk functions - :-(
However, I wanted to ask you again for clarification because this array I'm using is a simple array, not associative. And you are using a key/value pair here and I wanted to simplify this, since I don't want to skip the key but just the value; or if in a simple array means that key and value are the same, can we write this more simply?
My active var I want to skip is $currentForm, my array is $pageArray. In your follow up, you said " It MUST be the actual array key."
So, I'm getting confused. I checked the manual online, and it seems the key is only an associative property. Thanks Jeff
I'd rather be drawing...
|
|
Reply By:
|
jefferis
|
Reply Date:
|
11/27/2003 10:38:06 AM
|
echo "initial value: $foo"; // prints "initial value: 5" change_value($foo); echo "after change_value(): $foo"; // prints "after change_value(): 5"
also, shouldn't this be 'after change_ value (): 12345'?
I'd rather be drawing...
|