PHP How-ToPost your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
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.
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');
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!!"
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