Home »
Imported messages »
comp.lang.php »
Removing specific array items
Re: Removing specific array items [message #177972 is a reply to message #177969] |
Thu, 03 May 2012 16:05  |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
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
|
|
|
Goto Forum:
Current Time: Thu Feb 20 01:04:47 GMT 2025
Total time taken to generate the page: 0.04410 seconds