As far as I know, there is no built-in function for this, but you can use the following function:
function combineLinearArray( $arrayToSmush, $evenItemIsKey = true ) { if ( ( count($arrayToSmush) % 2 ) !== 0 ) { throw new Exception( "This array cannot be combined because it has an odd number of values" ); } $evens = $odds = array(); // Separate even and odd values for ($i = 0, $c = count($arrayToSmush); $i < $c; $i += 2) { $evens[] = $arrayToSmush[$i]; $odds[] = $arrayToSmush[$i+1]; } // Combine them and return return ( $evenItemIsKey ) ? array_combine($evens, $odds) : array_combine($odds, $evens); }
You can call this with the array you want to combine into an associative array, and an optional flag indicating whether to use even or odd elements as keys.
Edit: I modified the code to use only one for the loop, not a separate loop for extracting even and odd values.
source share