Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #5808 > unrolled thread
| Started by | Scott Johnson <noonehome@chalupasworld.com> |
|---|---|
| First post | 2012-05-03 06:25 -0700 |
| Last post | 2012-05-03 18:05 +0200 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.php
Removing specific array items Scott Johnson <noonehome@chalupasworld.com> - 2012-05-03 06:25 -0700
Re: Removing specific array items Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-03 10:55 -0400
Re: Removing specific array items Scott Johnson <noonehome@chalupasworld.com> - 2012-05-03 16:32 -0700
Re: Removing specific array items "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-03 17:20 +0200
Re: Removing specific array items Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-05-03 18:05 +0200
| From | Scott Johnson <noonehome@chalupasworld.com> |
|---|---|
| Date | 2012-05-03 06:25 -0700 |
| Subject | Removing specific array items |
| Message-ID | <jnu10f$v35$1@dont-email.me> |
In a simple quote cart I track items in a session.
$_SESSION['quote'][##] = {data}
Which increments the ## for each added item.
At the time when the client wants to remove an item from the cart I just
clear the {data} from $_SESSION['quote'][item_id] = '';
The issue i run into is when I want to display the cart, i just cant
step thru the array without seeing if a particular item is '', which
works but would like to find a more 'elegant' way.
With all that said, is there a way to remove the item from the array
rather then clear the items data.
I can only think to
1. 'somehow' move or rotate the item to the end of the array and POP it off
2. or even go as far as step thru the array and create a new one with
only populated data items.
I have looked thru the 50+ different array functions and can't seem to
find the right tool. Maybe a combination I am not seeing.
Thanks
Scotty
[toc] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2012-05-03 10:55 -0400 |
| Message-ID | <jnu68g$vjr$1@dont-email.me> |
| In reply to | #5808 |
On 5/3/2012 9:25 AM, Scott Johnson wrote:
> In a simple quote cart I track items in a session.
>
> $_SESSION['quote'][##] = {data}
>
> Which increments the ## for each added item.
>
> At the time when the client wants to remove an item from the cart I just
> clear the {data} from $_SESSION['quote'][item_id] = '';
>
> The issue i run into is when I want to display the cart, i just cant
> step thru the array without seeing if a particular item is '', which
> works but would like to find a more 'elegant' way.
>
> With all that said, is there a way to remove the item from the array
> rather then clear the items data.
>
> I can only think to
> 1. 'somehow' move or rotate the item to the end of the array and POP it off
> 2. or even go as far as step thru the array and create a new one with
> only populated data items.
>
> I have looked thru the 50+ different array functions and can't seem to
> find the right tool. Maybe a combination I am not seeing.
>
> Thanks
> Scotty
unset().
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Scott Johnson <noonehome@chalupasworld.com> |
|---|---|
| Date | 2012-05-03 16:32 -0700 |
| Message-ID | <jnv4i9$2fs$1@dont-email.me> |
| In reply to | #5809 |
On 5/3/2012 7:55 AM, Jerry Stuckle wrote:
> On 5/3/2012 9:25 AM, Scott Johnson wrote:
>> In a simple quote cart I track items in a session.
>>
>> $_SESSION['quote'][##] = {data}
>>
>> Which increments the ## for each added item.
>>
>> At the time when the client wants to remove an item from the cart I just
>> clear the {data} from $_SESSION['quote'][item_id] = '';
>>
>> The issue i run into is when I want to display the cart, i just cant
>> step thru the array without seeing if a particular item is '', which
>> works but would like to find a more 'elegant' way.
>>
>> With all that said, is there a way to remove the item from the array
>> rather then clear the items data.
>>
>> I can only think to
>> 1. 'somehow' move or rotate the item to the end of the array and POP
>> it off
>> 2. or even go as far as step thru the array and create a new one with
>> only populated data items.
>>
>> I have looked thru the 50+ different array functions and can't seem to
>> find the right tool. Maybe a combination I am not seeing.
>>
>> Thanks
>> Scotty
>
> unset().
>
Thanks all for the replies.
unset() it is.
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Date | 2012-05-03 17:20 +0200 |
| Message-ID | <a0fm2eFu5sU1@mid.uni-berlin.de> |
| In reply to | #5808 |
Am 03.05.2012 15:25, schrieb Scott Johnson:
> In a simple quote cart I track items in a session.
>
> $_SESSION['quote'][##] = {data}
>
> Which increments the ## for each added item.
>
> At the time when the client wants to remove an item from the cart I just clear the
> {data} from $_SESSION['quote'][item_id] = '';
>
> The issue i run into is when I want to display the cart, i just cant step thru the
> array without seeing if a particular item is '', which works but would like to find a
> more 'elegant' way.
>
> With all that said, is there a way to remove the item from the array rather then
> clear the items data.
unset($_SESSION['quote'][item_id]);
/Str.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-05-03 18:05 +0200 |
| Message-ID | <1401150.AFRqVoOGUt@PointedEars.de> |
| In reply to | #5808 |
Scott Johnson wrote:
> In a simple quote cart I track items in a session.
>
> $_SESSION['quote'][##] = {data}
>
> Which increments the ## for each added item.
ISTM you are looking for
$_SESSION['quote'][] = $data;
instead, which automatically increments the index of the item in
$_SESSION['quote']. On the other hand, if the real code was
$_SESSION['quote'][$index] = $items[$index];
you might want to consider simplifying the loop to one statement:
$_SESSION['quote'] = $items;
> At the time when the client wants to remove an item from the cart I just
> clear the {data} from $_SESSION['quote'][item_id] = '';
Consider
unset($_SESSION['quote'][$item_id]);
instead.
> The issue i run into is when I want to display the cart, i just cant
> step thru the array without seeing if a particular item is '',
(Please try to work on your spelling.)
> which works but would like to find a more 'elegant' way.
After the above, you might want to use
if (!isset($_SESSION['quote'][$item_id])
{
/* Item with $item_id exists */
}
This approach is more precise in combination with is_null().
> With all that said, is there a way to remove the item from the array
> rather then clear the items data.
^
That is _not_ a question.
> I have looked thru the 50+ different array functions and can't seem to
> find the right tool. Maybe a combination I am not seeing.
There is:
<http://php.net/unset>
<http://php.net/isset>
<http://php.net/is_null>
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web