how to change this array?

Discussion in 'PHP' started by w47w47, Oct 26, 2010.

  1. #1
    i want to change this array:

    $a=array('a','b','c','d','e','f','g');

    to this:

    $a=array('a','b','c');
     
    w47w47, Oct 26, 2010 IP
  2. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    ??? you would lose data
     
    max2010, Oct 26, 2010 IP
  3. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    no... i just need the first 3 entrys of the array.
     
    w47w47, Oct 26, 2010 IP
  4. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    something like this:

    for ($x=0; $x<3; $x++) {
    $b[$x]=$a[$x];
    }
    PHP:
    now you should have first 3 data of array $a[] into array $b[]
     
    max2010, Oct 26, 2010 IP
  5. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thank you max2010. lol so easy and i didn't knew it. :D >)
     
    w47w47, Oct 26, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    lukeg32, Oct 26, 2010 IP
  7. Sparaker

    Sparaker Peon

    Messages:
    204
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    array_slice is the correct and more efficient method saves you lines of code as well. see lukeg32's post. hands up.
     
    Sparaker, Oct 26, 2010 IP
  8. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    tnx lukeg32! ;)
     
    w47w47, Oct 26, 2010 IP
  9. reli4nt

    reli4nt Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Good call. Always try to keep php.net handy so you can find those efficiencies. They really do add up over time in maintenance and cpu load.
     
    reli4nt, Oct 27, 2010 IP