Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3

Thread: Arrays

  1. #1
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Let's say I have the following code:
    PHP Code:
    for ($i 0$i sizeof($myarray); $i++)
    {
        if (
    $myarray[$i] == "bad value") unset($myarray[$i]);

    That looks simple enough: delete every element in the NON-KEYED array $myarray. The problem is, will this also decrement every index beyond $i by one as a result of removing the value?

    If it does then I can't really think of a way to do this other than the cheap method of selectively copying the array, and PHP doesn't have a dedicated index/value deletion function.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  2.  

  3. #2
    Senior Member nsr81's Avatar
    Join Date
    Oct 2002
    Posts
    1,132
    Member #
    250
    Liked
    15 times
    Hehehe, I had this problem very recently. But not with PHP, with C++ in one of my class projects.

    Try this:
    PHP Code:
    $size sizeof($myarray);

    for (
    $i 0$i $size$i++)
    {
        if (
    $myarray[$i] == "bad value")
        {
           unset(
    $myarray[$i]);
           
    $size--;
        }

    There and Back Again :Ogre:

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Ah, good idea, trying that
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:24 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com