PHP Conference Kansai 2025

Voting

: five plus three?
(Example: nine)

The Note You're Voting On

randomdestination at gmail dot com
19 years ago
To split an associative array based on it's keys, use this function:

<?php
function &array_split(&$in) {
$keys = func_get_args();
array_shift($keys);

$out = array();
foreach(
$keys as $key) {
if(isset(
$in[$key]))
$out[$key] = $in[$key];
else
$out[$key] = null;
unset(
$in[$key]);
}

return
$out;
}
?>

Example:
<?php
$testin
= array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$testout =& array_split($testin, 'a', 'b', 'c');

print_r($testin);
print_r($testout);
?>

Will print:

Array
(
[d] => 4
)
Array
(
[a] => 1
[b] => 2
[c] => 3
)

Hope this helps anyone!

<< Back to user notes page

To Top