$candidates */ public static function pick(array $candidates, int $parent, string $language): ?int { $underParent = []; foreach ($candidates as $row) { if ((int) $row['parent'] === $parent) { $underParent[] = $row; } } if ($underParent === []) { return null; } if ($language === '') { return (int) $underParent[0]['id']; } foreach ($underParent as $row) { $termLanguage = (string) $row['language']; if ($termLanguage === '' || $termLanguage === $language) { return (int) $row['id']; } } foreach ($underParent as $row) { $translatedId = (int) ($row['translated_id'] ?? 0); $translatedParent = $row['translated_parent'] ?? null; if ($translatedId > 0 && $translatedParent !== null && (int) $translatedParent === $parent) { return $translatedId; } } return (int) $underParent[0]['id']; } }