TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   How to delete array element in function (http://www.talkphp.com/general/4085-how-delete-array-element-function.html)

benton 03-31-2009 04:22 PM

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); 


xenon 03-31-2009 05:34 PM

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]); 


benton 03-31-2009 06:11 PM

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.

Salathe 03-31-2009 06:41 PM

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)).

benton 04-02-2009 01:12 AM

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


All times are GMT. The time now is 01:10 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0