[PHP] removing an element from an array

2003-10-01 Thread Angelo Zanetti
Hi

If I have an array and want to remove and element from this array. correct
me if this is the wrong approach:

create a temporary array with 1 element less than the origional array, get
the position of the element ( is there a function for this?) and then run
through the origional array copying all elements besides the element which
is meant to be removed.

Is there no function to do this? It seems like alot of overhead to do this
process, especially if the array is big. comments?

Angelo

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] removing an element from an array

2003-10-01 Thread Ford, Mike [LSS]
On 01 October 2003 11:25, Angelo Zanetti contributed these pearls of wisdom:

 Hi
 
 If I have an array and want to remove and element from this
 array. correct me if this is the wrong approach:

It's the wrong approach ;)
 
 create a temporary array with 1 element less than the
 origional array, get the position of the element ( is there a
 function for this?) and then run through the origional array
 copying all elements besides the element which is meant to be
 removed. 
 
 Is there no function to do this? It seems like alot of
 overhead to do this process, especially if the array is big.
 comments? 

unset($array[$key]);

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] removing an element from an array

2003-10-01 Thread Angelo Zanetti
isnt the unset function not a safe function to use  was discontinued, i
think thats why its not in the manual.


-Original Message-
From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 01, 2003 12:58 PM
To: '[EMAIL PROTECTED]'; [EMAIL PROTECTED]
Subject: RE: [PHP] removing an element from an array


On 01 October 2003 11:25, Angelo Zanetti contributed these pearls of wisdom:

 Hi

 If I have an array and want to remove and element from this
 array. correct me if this is the wrong approach:

It's the wrong approach ;)

 create a temporary array with 1 element less than the
 origional array, get the position of the element ( is there a
 function for this?) and then run through the origional array
 copying all elements besides the element which is meant to be
 removed.

 Is there no function to do this? It seems like alot of
 overhead to do this process, especially if the array is big.
 comments?

unset($array[$key]);

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] removing an element from an array

2003-10-01 Thread Daniel Perez Clavero
Angelo,
Given array1 extract array2 from there, and put it into $array_result.

$array2[0]=Value to extract;
$array_result = array_diff ($array1, $array2);

Should work.
Rgrds.

-Original Message-
From: Angelo Zanetti [mailto:[EMAIL PROTECTED]
Sent: miércoles, 01 de octubre de 2003 12:25
To: [EMAIL PROTECTED]
Subject: [PHP] removing an element from an array


Hi

If I have an array and want to remove and element from this array. correct
me if this is the wrong approach:

create a temporary array with 1 element less than the origional array, get
the position of the element ( is there a function for this?) and then run
through the origional array copying all elements besides the element which
is meant to be removed.

Is there no function to do this? It seems like alot of overhead to do this
process, especially if the array is big. comments?

Angelo

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] removing an element from an array

2003-10-01 Thread Dennis Sterzenbach
Angelo Zanetti [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 isnt the unset function not a safe function to use  was discontinued,
i
 think thats why its not in the manual.

Hi Angelo,

look at the examples on
http://www.php.net/manual/en/function.unset.php
And you know that it is also used for purpose of deleting some
element from an array at some key.

--
 Dennis Sterzenbach
 www.darknoise.de

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] removing an element from an array

2003-10-01 Thread Dennis Sterzenbach
Daniel Perez Clavero [EMAIL PROTECTED] wrote in
message
news:[EMAIL PROTECTED]
 Angelo,
 Given array1 extract array2 from there, and put it into $array_result.

 $array2[0]=Value to extract;
 $array_result = array_diff ($array1, $array2);

 Should work.
 Rgrds.

Well this only returns the differences between $array1 and $array2.
But it doesn't touch/change the arrays themselves.

--
 Dennis Sterzenbach
 www.darknoise.de

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] removing an element from an array

2003-10-01 Thread Daniel Perez Clavero
Copy/Paste to a php file, and observe how it works. Maybe could be usefull
for u

?

$array1[0]=Red;
$array1[1]=Blue;
$array1[2]=White;
$array1[3]=Black;

$array2[0]=Blue;
$array2[1]=Black;

$array1_result_diff=array_diff($array1, $array2);

echo This is an array with colors not starting with B\B\/B;
echo BR;
echo var_dump($array1_result_diff);
echo BR;
echo BR;
echo BR;

$array2_result_diff=array_intersect($array1, $array2);
echo This is an array with colors starting with B\B\/B;
echo BR;
echo var_dump($array2_result_diff);
?



-Original Message-
From: Angelo Zanetti [mailto:[EMAIL PROTECTED]
Sent: miércoles, 01 de octubre de 2003 12:25
To: [EMAIL PROTECTED]
Subject: [PHP] removing an element from an array


Hi

If I have an array and want to remove and element from this array. correct
me if this is the wrong approach:

create a temporary array with 1 element less than the origional array, get
the position of the element ( is there a function for this?) and then run
through the origional array copying all elements besides the element which
is meant to be removed.

Is there no function to do this? It seems like alot of overhead to do this
process, especially if the array is big. comments?

Angelo

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] removing an element from an array

2003-10-01 Thread Daniel Perez Clavero
I know but was just a sample, try to use $array1 instead of $array_result.
Note: keys remain the same.

$array2[0]=Value to extract;
$array1 = array_diff ($array1, $array2);

-Original Message-
From: Dennis Sterzenbach [mailto:[EMAIL PROTECTED]
Sent: miércoles, 01 de octubre de 2003 13:24
To: [EMAIL PROTECTED]
Subject: Re: [PHP] removing an element from an array


Daniel Perez Clavero [EMAIL PROTECTED] wrote in
message
news:[EMAIL PROTECTED]
 Angelo,
 Given array1 extract array2 from there, and put it into $array_result.

 $array2[0]=Value to extract;
 $array_result = array_diff ($array1, $array2);

 Should work.
 Rgrds.

Well this only returns the differences between $array1 and $array2.
But it doesn't touch/change the arrays themselves.

--
 Dennis Sterzenbach
 www.darknoise.de

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php