Skip to content

Instantly share code, notes, and snippets.

@varemenos
Created March 17, 2012 16:30
Show Gist options
  • Save varemenos/2061835 to your computer and use it in GitHub Desktop.
Save varemenos/2061835 to your computer and use it in GitHub Desktop.
PHP - Compact Variables into an Array
<?php
$name = 'James';
$surname = 'Pond';
$job = 'Failure';
$temp = array('name', 'surname');
$result1 = compact($temp, 'job');
$result2 = compact($temp[0], $temp[1], 'job');
var_dump($result1);
// RESULTS
/*
// same result for both versions
array
'name' => string 'James' (length=5)
'surname' => string 'Pond' (length=4)
'job' => string 'Failure' (length=7)
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment