Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5811

Re: Removing specific array items

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="ISO-8859-1"
Message-ID <1401150.AFRqVoOGUt@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <php@PointedEars.de>
Organization PointedEars Software (PES)
Date Thu, 03 May 2012 18:05:59 +0200
User-Agent KNode/4.4.11
Content-Transfer-Encoding 7Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: Removing specific array items
Newsgroups comp.lang.php
References <jnu10f$v35$1@dont-email.me>
Followup-To comp.lang.php
MIME-Version 1.0
Lines 67
NNTP-Posting-Date 03 May 2012 18:06:00 CEST
NNTP-Posting-Host 9cd52f50.newsspool4.arcor-online.net
X-Trace DXC=GUR^[?Sli84lIh70@<QIE74IUK<Cl32<14Fo<]lROoR18kF<OcfhCO;Z60UQ4YM3<<DZm8W4\YJN<UFDj8T]K0?3DmniOZG;K0;Z4Tk8n04`e8
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.php:5811

Followups directed to: comp.lang.php

Show key headers only | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web