Untitled Diff

Created Diff never expires
19 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
23 lines
21 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
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;
}
}