SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: array questions

  1. #1
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array questions

    hello

    is there a way to delete a number of items in an array that works a bit like array_slice ... array_slice ($myarray, 0, 2)
    ie delete items from array from position 0 to the length of 2

    and is there a way to reset the keys when the items are deleted ie if i delete item 0 to 3 in an array such as here...

    Array ( [0] => a [1] => b [2] => c [3] => d [4] => d)

    how can i reset the fourth item to be now 0???

    any help much appreciated
    a+
    Garrett

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2000
    Location
    Yeppoon, Australia
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do it (from http://www.php.net/manual/en/function.array-slice.php)

    PHP Code:
    if you want to "securely" remove an element of an array .... 

    $objects =
    array(
    'first','second','third','fourth','fifth','sixth','seventteen'); 

    function 
    remove_element($array$left_id$right_id// Remove Element 
    // left_id is element position(counts from 1) 
    // right_id array position (counts from 0) 


    $left array_slice ($array0$left_id); 
    $right array_slice ($array$right_id); 

    $array array_merge ($left$right); 

    unset (
    $left); 
    unset (
    $right); 

    return 
    $array


    // just give both information of the element you want to remove. 
    // if youi want to remove the third element transmit the array 
    // position (2) and the "real" position (3) to the function, 
    // and the element is gone ... 

    $objects remove_element($objects2,3); 
    but after looking here : http://www.php.net/manual/en/function.array-splice.php

    there's probably a better way listed (in the user posts). Check them bot out and see what you want to use
    Last edited by mr_than; Nov 14, 2001 at 10:55.
    Knowledge is knowing that a tomatoe is a fruit; wisdom is not putting it in a fruit salad.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •