Untitled Diff
19 removals
Words removed | 32 |
Total words | 94 |
Words removed (%) | 34.04 |
23 lines
21 additions
Words added | 40 |
Total words | 102 |
Words added (%) | 39.22 |
21 lines
<?php
<?php
// Turn an array into a key=>value pair. Assumes the key is the first item in the sub-array.
// Turn an array into a key=>value or key=>array pair.
public function column_into_keys(array $array): array {
// Assumes the key is the first item in the sub-array.
public function columnIntoKeys(array $array): array {
// get the name of the column that contains the record id
$key = key($array[0]);
// skip empty arrays
if (! count($array)) return [];
// loop through each result row
foreach($array as $row) {
foreach($array as $row) {
// pop the new key off the top of the array
// pop the value of the row identifier off the top of the array
$id = array_shift($row);
$id = array_shift($row); // O(n)
// is there only one item left in the array?
// if there is only one item left in the array...
if (count($row) == 1)
$result[$id] = (count($row) == 1)
// get the first value
? current($row) // ...get the first value
$result[$id] = current($row);
: $row; // ...get the rest of the array
else
// get all of the values
$result[$id] = $row;
}
}
return $result;
return $result;
}
}