Re: [PHP] Array function to delete

2002-04-26 Thread Liam Gibbs

> But PHP builtins will do exactly this, around 3
> times faster, too. :)

Yup. This one with array_diff was the winner. Thanks
to all for your suggestions and Lars for the working
one.

Liam


__
Do You Yahoo!?
Yahoo! Games - play chess, backgammon, pool and more
http://games.yahoo.com/

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




RE: [PHP] Array function to delete

2002-04-26 Thread Ford, Mike [LSS]

> -Original Message-
> From: Nathan [mailto:[EMAIL PROTECTED]]
> Sent: 25 April 2002 21:01
> 
> Apparently in 4.1.X this is true... I'd been running 4.0.6 
> for a long time (until 4.1.2 was
> released), and must have coded it in the earlier version. I 
> stand corrected! :-)

H'mmm, I think you must have an even longer memory than that, since the following 
script:

>  $array = array();
> $array[0] = "zero";
> $array[1] = "one";
> $array[2] = "two";
> $array[3] = "three";
> 
> unset($array[1]);
> 

  echo "version=" . phpversion() . "";

> foreach ($array as $key=>$value) {
> echo $key." = ".$value."";
> }
> ?>

when run on my server produces:

version=4.0.5
0 = zero
2 = two
3 = three

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] Array function to delete

2002-04-25 Thread Lars Torben Wilson

On Thu, 2002-04-25 at 14:01, Kevin Stone wrote:
> About all I would suggest is that you eliminate the unecessary function
> calls substr and explode to optimize your code.  I doubt prebuilt PHP
> functions will help you here.  This is perfectly valid method for deleting
> elements in an array and it's about as fast as you're ever going to get.  :)

But PHP builtins will do exactly this, around 3 times faster, too. :)



If you need the array reindexed, just array_values() it when you're 
done.

>  $listitems = array ('grill', 'bar', 'foo', 'world');
> $item_to_delete = 'foo';
> 
> // Loop through listitems array.
> for ($i=0; $i {
> // Ignore item to delete.
> if ($listitems[$i] != $item_to_delete)
> {
> // Save all other items in tmplist array.
> $tmplist[] = $listitems[$i];
> }
> }
> $listitems = $tmplist;  //new array contains (grill, bar, world)
> ?>
> 
> -Kevin
> 
> - Original Message -
> From: "Liam Gibbs" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, April 25, 2002 2:27 PM
> Subject: Re: [PHP] Array function to delete
> 
> 
> > Thanks for all your help, everyone, but both
> > suggestions (unset and array_slice) pretty much didn't
> > improve on my current way. I was jsut trying to find a
> > faster way, but unset doesn't seem to be working
> > properly (but I'll need to fiddle more) and the
> > array_slice way is just too intensive.
> >
> > for($counter = 0; $counter < count($listitems);
> > $counter++) {
> > if($listitems[$counter] != $item)
> > $newlist .= ",$listitems[$counter]";
> > }
> > $newlist = substr($newlist, 1, strlen($newlist));
> > $listitems = explode("," $newlist);
> >
> > __
> > Do You Yahoo!?
> > Yahoo! Games - play chess, backgammon, pool and more
> > http://games.yahoo.com/
> >
> > --
> > 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
> 
-- 
 Torben Wilson <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


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




Re: [PHP] Array function to delete

2002-04-25 Thread Kevin Stone

About all I would suggest is that you eliminate the unecessary function
calls substr and explode to optimize your code.  I doubt prebuilt PHP
functions will help you here.  This is perfectly valid method for deleting
elements in an array and it's about as fast as you're ever going to get.  :)



-Kevin

- Original Message -
From: "Liam Gibbs" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, April 25, 2002 2:27 PM
Subject: Re: [PHP] Array function to delete


> Thanks for all your help, everyone, but both
> suggestions (unset and array_slice) pretty much didn't
> improve on my current way. I was jsut trying to find a
> faster way, but unset doesn't seem to be working
> properly (but I'll need to fiddle more) and the
> array_slice way is just too intensive.
>
> for($counter = 0; $counter < count($listitems);
> $counter++) {
> if($listitems[$counter] != $item)
> $newlist .= ",$listitems[$counter]";
> }
> $newlist = substr($newlist, 1, strlen($newlist));
> $listitems = explode("," $newlist);
>
> __
> Do You Yahoo!?
> Yahoo! Games - play chess, backgammon, pool and more
> http://games.yahoo.com/
>
> --
> 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] Array function to delete

2002-04-25 Thread Erik Price


On Thursday, April 25, 2002, at 04:27  PM, Liam Gibbs wrote:

> Thanks for all your help, everyone, but both
> suggestions (unset and array_slice) pretty much didn't
> improve on my current way. I was jsut trying to find a
> faster way, but unset doesn't seem to be working
> properly (but I'll need to fiddle more) and the
> array_slice way is just too intensive.
>
> for($counter = 0; $counter < count($listitems);
> $counter++) {
>   if($listitems[$counter] != $item)
>   $newlist .= ",$listitems[$counter]";
> }
> $newlist = substr($newlist, 1, strlen($newlist));
> $listitems = explode("," $newlist);

Why not just do

$newlistitems = array(); // initialize variable
foreach ($listitems as $listitem) {
if ($listitem != $item) {
$newlistitems[] = $listitem;
}
}

This gives you an array called $newlistitems.  Each element in 
$newlistitems is an element from the old $listitems that does not match 
$item.


Erik






Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


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




Re: [PHP] Array function to delete

2002-04-25 Thread Liam Gibbs

Thanks for all your help, everyone, but both
suggestions (unset and array_slice) pretty much didn't
improve on my current way. I was jsut trying to find a
faster way, but unset doesn't seem to be working
properly (but I'll need to fiddle more) and the
array_slice way is just too intensive.

for($counter = 0; $counter < count($listitems);
$counter++) {
if($listitems[$counter] != $item)
$newlist .= ",$listitems[$counter]";
}
$newlist = substr($newlist, 1, strlen($newlist));
$listitems = explode("," $newlist);

__
Do You Yahoo!?
Yahoo! Games - play chess, backgammon, pool and more
http://games.yahoo.com/

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




Re: [PHP] Array function to delete

2002-04-25 Thread Richard Baskett

OK I wrote this function to delete a specific value in an array, it
shouldn¹t be hard to modify it to do what you need it to do.  $num is the
value I want to delete, $cartVals is the array.  If you have any questions
feel free to contact me.  Cheers!

function cartRem($num) {
  global $cartVals;
  $cartVals[(array_search($num, $cartVals))] = NULL;
  sort($cartVals);
  array_shift($cartVals);
  cartSave($cartVals);
  return "Program #$num has been Removed from your shopping cart";
}

Rick

"I count him braver who overcomes his desires than him who conquers his
enemies; for the hardest victory is over self." - Aristotle

> From: Lars Torben Wilson <[EMAIL PROTECTED]>
> Date: 26 Apr 2002 12:50:03 -0700
> To: Pushkar Pradhan <[EMAIL PROTECTED]>
> Cc: Erik Price <[EMAIL PROTECTED]>, Liam Gibbs <[EMAIL PROTECTED]>,
> [EMAIL PROTECTED]
> Subject: Re: [PHP] Array function to delete
> 
> On Thu, 2002-04-25 at 12:32, Pushkar Pradhan wrote:
>> That's neat but I just read the docs. it says unset() won't work inside a
>> function, even if you pass by reference!
> 
> No, that is not at all what it says.
> 
>> Anyways as far as you don't use functions to do the delete it will work
>> fine.
>>> On Thursday, April 25, 2002, at 03:22  PM, Liam Gibbs wrote:
>>> 
>>>> I've been checking the PHP documentation, but can't
>>>> find a function that will delete a member of an array,
>>>> like such:
>>>> 
>>>> $a = array(1, 2, 3, 4, 5);
>>>> 
>>>> Use the function, say array_delete($a, 3); and that
>>>> will delete the third member in the array (which would
>>>> be 4 above), so that the array would contain 1, 2, 3,
>>>> 5.
>>>> 
>>>> Is there such a function?
>>> 
>>> It's a secret.  Sshh!
>>> 
>>> unset($a[0]);  // removes the first element
>>> 
>>> 
>>> 
>>> Erik
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> Erik Price
>>> Web Developer Temp
>>> Media Lab, H.H. Brown
>>> [EMAIL PROTECTED]
>>> 
>>> 
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>> 
>> 
>> -Pushkar S. Pradhan
>> 
>> 
>> -- 
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>> 
> -- 
> Torben Wilson <[EMAIL PROTECTED]>
> http://www.thebuttlesschaps.com
> http://www.hybrid17.com
> http://www.inflatableeye.com
> +1.604.709.0506
> 
> 
> -- 
> 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] Array function to delete

2002-04-25 Thread Nathan

Apparently in 4.1.X this is true... I'd been running 4.0.6 for a long time (until 
4.1.2 was
released), and must have coded it in the earlier version. I stand corrected! :-)

A simple test to see if your version supports this:

$value) {
echo $key." = ".$value."";
}
?>

If you're running an older version, it should output:
0 = zero
1 =
2 = two
3 = three

Versions 4.1.X+ should completely remove key 1.

Cheers,

# Nathan

- Original Message -
From: "Lars Torben Wilson" <[EMAIL PROTECTED]>
To: "Nathan" <[EMAIL PROTECTED]>
Cc: "Liam Gibbs" <[EMAIL PROTECTED]>; "PHP" <[EMAIL PROTECTED]>
Sent: Friday, April 26, 2002 1:46 PM
Subject: Re: [PHP] Array function to delete


On Thu, 2002-04-25 at 12:34, Nathan wrote:
> No. :-)
>
> What I've had to do is add a dummy value, unset($array[4]) and then array_pop the 
>dummy value off
> the array to get it out of there completely. The reason: unset will only remove 
>$array[4]'s value,
> not the key.
>
> There are plenty of other ways I'm sure, but simply unsetting has really messed me 
>up on occasion.
> I'll unset a value and then implode my array (say a list of numbers), and end up 
>with a string
like
> so:
>
> 1,233,642,,234,5632,
>
> Where I've unset the 4th and last keys. Which of course really messes up an explode.
>
> # Nathan

You need to upgrade. :) The above hasn't been true for a while...at
least since 4.1.2 and perhaps earlier.



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




Re: [PHP] Array function to delete

2002-04-25 Thread Lars Torben Wilson

On Thu, 2002-04-25 at 12:32, Pushkar Pradhan wrote:
> That's neat but I just read the docs. it says unset() won't work inside a
> function, even if you pass by reference!

No, that is not at all what it says.

> Anyways as far as you don't use functions to do the delete it will work
> fine.
> > On Thursday, April 25, 2002, at 03:22  PM, Liam Gibbs wrote:
> >
> > > I've been checking the PHP documentation, but can't
> > > find a function that will delete a member of an array,
> > > like such:
> > >
> > > $a = array(1, 2, 3, 4, 5);
> > >
> > > Use the function, say array_delete($a, 3); and that
> > > will delete the third member in the array (which would
> > > be 4 above), so that the array would contain 1, 2, 3,
> > > 5.
> > >
> > > Is there such a function?
> >
> > It's a secret.  Sshh!
> >
> > unset($a[0]);  // removes the first element
> >
> >
> >
> > Erik
> >
> >
> >
> >
> > 
> >
> > Erik Price
> > Web Developer Temp
> > Media Lab, H.H. Brown
> > [EMAIL PROTECTED]
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> 
> -Pushkar S. Pradhan
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
-- 
 Torben Wilson <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


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




Re: [PHP] Array function to delete

2002-04-25 Thread Lars Torben Wilson

On Thu, 2002-04-25 at 12:34, Nathan wrote:
> No. :-)
> 
> What I've had to do is add a dummy value, unset($array[4]) and then array_pop the 
>dummy value off
> the array to get it out of there completely. The reason: unset will only remove 
>$array[4]'s value,
> not the key.
> 
> There are plenty of other ways I'm sure, but simply unsetting has really messed me 
>up on occasion.
> I'll unset a value and then implode my array (say a list of numbers), and end up 
>with a string like
> so:
> 
> 1,233,642,,234,5632,
> 
> Where I've unset the 4th and last keys. Which of course really messes up and explode.
> 
> # Nathan

You need to upgrade. :) The above hasn't been true for a while...at 
least since 4.1.2 and perhaps earlier.


> - Original Message -
> From: "Liam Gibbs" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, April 25, 2002 1:22 PM
> Subject: [PHP] Array function to delete
> 
> 
> I've been checking the PHP documentation, but can't
> find a function that will delete a member of an array,
> like such:
> 
> $a = array(1, 2, 3, 4, 5);
> 
> Use the function, say array_delete($a, 3); and that
> will delete the third member in the array (which would
> be 4 above), so that the array would contain 1, 2, 3,
> 5.
> 
> Is there such a function?
> 
> __
> Do You Yahoo!?
> Yahoo! Games - play chess, backgammon, pool and more
> http://games.yahoo.com/
> 
> --
> 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
> 
> 
-- 
 Torben Wilson <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


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




Re: [PHP] Array function to delete

2002-04-25 Thread Pushkar Pradhan


$layer is my array -
 $currPos = array_search($HTTP_POST_VARS["selLayer"], $layer);
 $arrB = array_slice($layer, $currPos);
 $val  = array_shift($arrB);
 $arrA = array_slice($layer, 0, $currPos);
 $layer = array_merge($arrA, $arrB);
 // I do this to change the position of the element actually
 // so you can comment this out
 // array_unshift($layer, $val);
It's little memory expensive since it will create two more arrays but you
can use unset or something to delete them, will work inside a function.


> No. :-)
>
> What I've had to do is add a dummy value, unset($array[4]) and then array_pop the 
>dummy value off
> the array to get it out of there completely. The reason: unset will only remove 
>$array[4]'s value,
> not the key.
>
> There are plenty of other ways I'm sure, but simply unsetting has really messed me 
>up on occasion.
> I'll unset a value and then implode my array (say a list of numbers), and end up 
>with a string like
> so:
>
> 1,233,642,,234,5632,
>
> Where I've unset the 4th and last keys. Which of course really messes up and explode.
>
> # Nathan
>
> - Original Message -
> From: "Liam Gibbs" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, April 25, 2002 1:22 PM
> Subject: [PHP] Array function to delete
>
>
> I've been checking the PHP documentation, but can't
> find a function that will delete a member of an array,
> like such:
>
> $a = array(1, 2, 3, 4, 5);
>
> Use the function, say array_delete($a, 3); and that
> will delete the third member in the array (which would
> be 4 above), so that the array would contain 1, 2, 3,
> 5.
>
> Is there such a function?
>
> __
> Do You Yahoo!?
> Yahoo! Games - play chess, backgammon, pool and more
> http://games.yahoo.com/
>
> --
> 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
>

-Pushkar S. Pradhan


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




Re: [PHP] Array function to delete

2002-04-25 Thread Nathan

No. :-)

What I've had to do is add a dummy value, unset($array[4]) and then array_pop the 
dummy value off
the array to get it out of there completely. The reason: unset will only remove 
$array[4]'s value,
not the key.

There are plenty of other ways I'm sure, but simply unsetting has really messed me up 
on occasion.
I'll unset a value and then implode my array (say a list of numbers), and end up with 
a string like
so:

1,233,642,,234,5632,

Where I've unset the 4th and last keys. Which of course really messes up and explode.

# Nathan

- Original Message -
From: "Liam Gibbs" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, April 25, 2002 1:22 PM
Subject: [PHP] Array function to delete


I've been checking the PHP documentation, but can't
find a function that will delete a member of an array,
like such:

$a = array(1, 2, 3, 4, 5);

Use the function, say array_delete($a, 3); and that
will delete the third member in the array (which would
be 4 above), so that the array would contain 1, 2, 3,
5.

Is there such a function?

__
Do You Yahoo!?
Yahoo! Games - play chess, backgammon, pool and more
http://games.yahoo.com/

--
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] Array function to delete

2002-04-25 Thread Lars Torben Wilson

On Thu, 2002-04-25 at 12:22, Liam Gibbs wrote:
> I've been checking the PHP documentation, but can't
> find a function that will delete a member of an array,
> like such:
> 
> $a = array(1, 2, 3, 4, 5);
> 
> Use the function, say array_delete($a, 3); and that
> will delete the third member in the array (which would
> be 4 above), so that the array would contain 1, 2, 3,
> 5.
> 
> Is there such a function?

  http://www.php.net/unset


-- 
 Torben Wilson <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


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




Re: [PHP] Array function to delete

2002-04-25 Thread Pushkar Pradhan

That's neat but I just read the docs. it says unset() won't work inside a
function, even if you pass by reference!
Anyways as far as you don't use functions to do the delete it will work
fine.
>
> On Thursday, April 25, 2002, at 03:22  PM, Liam Gibbs wrote:
>
> > I've been checking the PHP documentation, but can't
> > find a function that will delete a member of an array,
> > like such:
> >
> > $a = array(1, 2, 3, 4, 5);
> >
> > Use the function, say array_delete($a, 3); and that
> > will delete the third member in the array (which would
> > be 4 above), so that the array would contain 1, 2, 3,
> > 5.
> >
> > Is there such a function?
>
> It's a secret.  Sshh!
>
> unset($a[0]);  // removes the first element
>
>
>
> Erik
>
>
>
>
> 
>
> Erik Price
> Web Developer Temp
> Media Lab, H.H. Brown
> [EMAIL PROTECTED]
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-Pushkar S. Pradhan


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




Re: [PHP] Array function to delete

2002-04-25 Thread Pushkar Pradhan

As far as I know there isn't such a function, what I do to achieve this:
1. find the position of the element you want to remove
2. slice the array into 2 arrays one contains all elements before this and
one contains all elements below this,
3. use shift to remove appropriate element
4. merge both arrays back.
Do you want the code?
> I've been checking the PHP documentation, but can't
> find a function that will delete a member of an array,
> like such:
>
> $a = array(1, 2, 3, 4, 5);
>
> Use the function, say array_delete($a, 3); and that
> will delete the third member in the array (which would
> be 4 above), so that the array would contain 1, 2, 3,
> 5.
>
> Is there such a function?
>
> __
> Do You Yahoo!?
> Yahoo! Games - play chess, backgammon, pool and more
> http://games.yahoo.com/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-Pushkar S. Pradhan


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




Re: [PHP] Array function to delete

2002-04-25 Thread Erik Price


On Thursday, April 25, 2002, at 03:22  PM, Liam Gibbs wrote:

> I've been checking the PHP documentation, but can't
> find a function that will delete a member of an array,
> like such:
>
> $a = array(1, 2, 3, 4, 5);
>
> Use the function, say array_delete($a, 3); and that
> will delete the third member in the array (which would
> be 4 above), so that the array would contain 1, 2, 3,
> 5.
>
> Is there such a function?

It's a secret.  Sshh!

unset($a[0]);  // removes the first element



Erik






Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]


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