Compress a php array

Posted by Derek Adair on Stack Overflow See other posts from Stack Overflow or by Derek Adair
Published on 2010-04-14T15:31:40Z Indexed on 2010/04/14 15:33 UTC
Read the original article Hit count: 144

Filed under:
|

I need to take an array that looks something like ...

array( 11 => "fistVal", 19 => "secondVal", 120=> "thirdVal", 200 =>"fourthVal");

and convert it to...

array( 0 => "fistVal", 1 => "secondVal", 2=> "thirdVal", 3 =>"fourthVal");

This is what I came up with -

function compressArray($array){
    if(count($array){
        $counter = 0;
        $compressedArray = array();
        foreach($array as $cur){
            $compressedArray[$count] = $cur;
            $count++;   
        }
        return $compressedArray;
    } else {
        return false;
    }
}

I'm just curious if there is any built-in functionality in php or neat tricks to do this.

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays