3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function splitDatabaseArray($array) { ksort($array); $return = array(); $last_database_table = array(); foreach ($array as $index => $value) { $database_table = array(); list($database_table[0], $database_table[1]) = explode('.', $index); if ($last_database_table != $database_table) $row = &$return[]; $last_database_table = $database_table; $row[$index] = $value; } return $return; } $array = array ( 'database1.table1.fieldname' => "test", 'database1.table1.fieldname1' => "test1", 'database2.table2.fieldname' => "test2"); var_dump(splitDatabaseArray($array));
Output for 5.2.1 - 7.1.0
array(2) { [0]=> array(2) { ["database1.table1.fieldname"]=> string(4) "test" ["database1.table1.fieldname1"]=> string(5) "test1" } [1]=> array(1) { ["database2.table2.fieldname"]=> string(5) "test2" } }
Output for 4.3.0 - 5.2.0
array(2) { [0]=> &array(2) { ["database1.table1.fieldname"]=> string(4) "test" ["database1.table1.fieldname1"]=> string(5) "test1" } [1]=> array(1) { ["database2.table2.fieldname"]=> string(5) "test2" } }