Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP How-To Post 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 26th, 2003, 03:10 PM
Authorized User
 
Join Date: Nov 2003
Location: Butler, PA, USA.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jefferis Send a message via AIM to jefferis Send a message via MSN to jefferis
Default Remove item from an array?

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...
__________________
~~~~~~~~~~~~~
 http://www.petersonsales.net
Reply With Quote
  #2 (permalink)  
Old November 26th, 2003, 04:20 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #3 (permalink)  
Old November 26th, 2003, 05:48 PM
Authorized User
 
Join Date: Nov 2003
Location: Butler, PA, USA.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jefferis Send a message via AIM to jefferis Send a message via MSN to jefferis
Default

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 With Quote
  #4 (permalink)  
Old November 26th, 2003, 07:18 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 With Quote
  #5 (permalink)  
Old November 27th, 2003, 10:29 AM
Authorized User
 
Join Date: Nov 2003
Location: Butler, PA, USA.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jefferis Send a message via AIM to jefferis Send a message via MSN to jefferis
Default

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 With Quote
  #6 (permalink)  
Old November 27th, 2003, 10:38 AM
Authorized User
 
Join Date: Nov 2003
Location: Butler, PA, USA.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jefferis Send a message via AIM to jefferis Send a message via MSN to jefferis
Default

  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...
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete item from array sektor Excel VBA 16 April 30th, 2008 04:36 AM
Remove ItemTemplate Item - Not EditItemTemplate rsearing ASP.NET 2.0 Basics 0 July 24th, 2007 08:41 PM
Deleting an Item from an Array creejohnson Classic ASP Professional 5 September 14th, 2006 12:30 PM
Getting an item from the array monfu C# 0 March 29th, 2006 08:21 AM
Find Out First Item of each item Group Jane SQL Language 1 November 22nd, 2003 11:42 AM



All times are GMT -4. The time now is 04:34 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.