3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $input = array( 0 => 'fruit', 1 => 'apple', 2 => 'vegetable', 3 => 'corn', ); /* remove last item from odd arrays */ if( count( $input ) % 2 != 0 ) { array_pop( $input ); } $output = array(); $inputLength = count( $input ); for( $i = 0; $i < $inputLength; $i = $i + 2 ) { $output[$input[$i]] = $input[$i + 1]; } var_dump( $output );
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(2) { ["fruit"]=> string(5) "apple" ["vegetable"]=> string(4) "corn" }