1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Number of an item in an array

Discussion in 'PHP' started by blackburn2413, Jan 27, 2011.

  1. #1
    Hey everyone, I have a list of results that I am storing in an array after I run the SQL query. There were a lot of duplicates in the array so I used the clean() function to resolve that. That worked just as I wanted, except for one thing: As seen below, the numbers of each item are 0,1, then 13. Is there any way to make this numbered from 0-2? I was thinking implode the array to a string then re-import to a new array to renumber everything but wasn't having luck there.

    Array ( [0] => Outing: basketball outing [1] => Outing: test outing [13] => Outing: family)
    Code (markup):
    Anyone have any ideas?
     
    blackburn2413, Jan 27, 2011 IP
  2. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #2
    foreach($yourarray as $ya => $value)
    {
     $newarray[] = $value;
    }
    
    
    PHP:
    this should work
     
    G3n3s!s, Jan 27, 2011 IP
  3. Ardra

    Ardra Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i support upper code
     
    Ardra, Jan 28, 2011 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Assuming the original array is structured as in your initial post (throughout), you can do the following:

    $formatted_array = array_values($original_array);
    PHP:
    Which saves you from looping.
     
    danx10, Jan 28, 2011 IP
  5. Mareshal

    Mareshal Member

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #5
    doesn't really save you from a loop. Sometimes a loop is faster than a PHP function ;)
     
    Mareshal, Jan 28, 2011 IP
  6. jsimpson

    jsimpson Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Interesting.. never test this out..
     
    jsimpson, Jan 28, 2011 IP
  7. irulsastra

    irulsastra Member

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    fast and professional in working very recommended :)
     
    irulsastra, Jan 29, 2011 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    Well that only applies sometimes, when what you want to achieve is very basic, looping wouldn't be an issue.

    However in the case, if you really want to consider speed, do a benchmark and let me know how that goes - I'd be interested to see which approach is faster. :)
     
    danx10, Jan 29, 2011 IP
  9. JohnRazmus

    JohnRazmus Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'd be interested in knowing too - I've always done it the array_values way.

    $quantity = count( array_values( $array ) );
     
    JohnRazmus, Jan 29, 2011 IP