TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-31-2009, 04:22 PM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default How to delete array element in function

I'm trying to delete an array element in a function. The manual for unset says to use $GLOBALS but that isn't working for some reason. The following is basically what I am doing. But when I run it, the original array has not been changed. Would someone please point out my mistake?
PHP Code:
function DeleteElement($id$myArray)
{
  for (
$i=0$i count($myArray); ++$i)
  {
     if (
$id == $myArray[$i]['id'])
     {  
        unset(
$GLOBALS["myArray[$i]"]);
     }
  }
}          

$myArray 
 Array
(
    [
0] => Array
        (
            [
id] => 1988
        
)

    [
1] => Array
        (
            [
id] => 3444
        
)
}

DeleteElement(1988$myArray); 
benton is offline  
Reply With Quote
Old 03-31-2009, 05:34 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Well...that's not good. I don't know who told you this, but it's completely wrong. The reason is not found in the code, hower, but in the PHP engine itself. It instructs functions that take as arguments arrays, strings, integers,etc. (not objects since PHP5), to make a copy of the original variable and use that when it does stuff with it in the function body. That's why GLOBALS didn't work. You didn't even need them. All you needed to do was declare the function parameter as a reference, not as a variable:

PHP Code:
function myFunction(&$array)
{
    for(
$i 0$i count($array); $i++)
    {
        if(
condition) unset($array[$i]);
    }

I don't see why you would need this function to delete an element of the array, since you can do it right in place,where you need it. It's unefficient what you do. Here's a better replacement code for that function:

PHP Code:
if(array_key_exists($i$myArray) === true && $id == $myArray[$i]['id']) unset($myArray[$i]); 
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 03-31-2009, 06:11 PM   #3 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Thanks for the explanation. I didn't use call by reference because when I've tried it before with other code, I always got a message that call by reference has been deprecated, with some php versions. So I don't use it now to avoid that problem. Is that not correct?

I used the $GLOBALS method since that is what it says to do in the php manual - http://us.php.net/unset

The function I posted was just for simplicity. The code involves more than that and it makes it easier to use a function in this case.
benton is offline  
Reply With Quote
Old 03-31-2009, 06:41 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

With regards to the original post, your use of $GLOBALS was incorrect. The correction would be unset($GLOBALS['myArray'][$i]);

Quote:
Originally Posted by xenon
Well...that's not good. I don't know who told you this, but it's completely wrong. The reason is not found in the code, hower, but in the PHP engine itself.
The reason is found in the code, not the PHP engine. Passing the array by reference or value is not really the key point; the problem was accessing the correct structure in the $GLOBALS array.

However, it would probably be more beneficial to start over and work with a reference to the array within the function as it would alleviate the need to have the global variable always named $myArray.

The warnings about call-time pass-by-reference being deprecated are only raised when you try to pass an argument as a reference when calling a function (e.g. $blah = myfunc(&$array)) and not when defining a function as accepting arguments by reference (e.g. function myfunc(&$array)).
Salathe is offline  
Reply With Quote
Old 04-02-2009, 01:12 AM   #5 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Thank you very much. :) That not only fixed the problem, your explanation cleared up some other problems I been fighting with.
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
function that returns an array -- why not working? Dave Absolute Beginners 3 08-13-2008 05:01 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 04:58 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design