I was wondering if its possible, to loop through an array but have each key as a variable?
My current code is below, with an example expected output:
<?php
$arr = array(array('id' => 24, 'name' => 'luigi'), array('id' => 12, 'name' => 'luiginsd'));
foreach ($arr as $value) {
echo $id . '<br />';
}
/*
which would output:
24<br />
12<br />
*/
?>
All help is appreciated.
Use
extract:extractwill iterate through an associative array and initialize a variable (presumably using variable variables) of the same name as the key in the array in the current scope containing the associated value.Just for fun, here’s what I think
extractdoes under the hood:Note that
extractdoes not necessarily import these variables into global scope, they are imported into the current symbol table.