[...]]. * This extracts the inner array for the given table key. */ public static function unwrap(?array $row, string $tableKey): ?array { if (!$row) { return null; } return $row[$tableKey] ?? null; } /** * Unwrap a list of DB rows from their table-name wrappers. */ public static function unwrapList(mixed $rows, string $tableKey): array { if (!is_array($rows)) { return []; } $list = []; foreach ($rows as $row) { $data = $row[$tableKey] ?? null; if (is_array($data)) { $list[] = $data; } } return $list; } /** * Extract integer IDs from DB rows, unwrapping the table-name key. * * Returns a deduplicated array of unique positive integer IDs. */ public static function extractIds(mixed $rows, string $tableKey, string $idField = 'id'): array { if (!is_array($rows)) { return []; } $ids = []; foreach ($rows as $row) { $data = $row[$tableKey] ?? $row; if (is_array($data) && isset($data[$idField])) { $ids[] = (int) $data[$idField]; } } return array_values(array_unique($ids)); } /** * Sanitize an array of IDs to unique positive integers. */ public static function sanitizePositiveIds(array $ids): array { $ids = array_values(array_unique(array_map('intval', $ids))); return array_values(array_filter($ids, static fn (int $id): bool => $id > 0)); } /** * Flatten a MintyPHP DB row that can contain nested table-key arrays. * * @return array */ public static function flattenRow(mixed $row): array { if (!is_array($row)) { return []; } $flat = []; foreach ($row as $key => $value) { if (is_array($value)) { $flat = array_merge($flat, $value); } elseif (is_string($key)) { $flat[$key] = $value; } } return $flat; } }