Home > Bookmarks, PhP, Tech > Trunc or Resize Array Size in PhP

Trunc or Resize Array Size in PhP

September 9th, 2008 Leave a comment Go to comments

If you want to modify the size of an array, larger or smaller than its current size, use array_pad() and array_splice() function.

How to make an array grow:

/***** start with 2 elements *****/
$array = array(’element1′, ‘element2′);

/***** grow to 4 elements *****/
$array = array_pad($array, 4, ”);

How to reduce an array:

/***** shrink to three elements ***** /
array_splice($array, 3);
or
/***** remove last element, equivalent to array_pop( ) *****/
array_splice($array, -1);

  1. David
    March 12th, 2009 at 05:39 | #1

    Hmmm… thanks. I was confused about this… good post

  1. No trackbacks yet.
GoCache - ByREV-Cache v1.0 - live served in : 0.16831 sec (gzip)